我目前正在尝试使用 ManagedInstallerClass.InstallHelper 安装多项服务。
当我安装第一个服务时,代码执行很顺利,然后出现错误,指出同名服务已存在。 但是,如果我退出程序然后从第二个服务开始执行相同的过程,一切都会顺利进行。
我用的函数就是这个
ManagedInstallerClass.InstallHelper(arguments.ToArray());
我 100% 确定参数是正确的。
我有一个异常(exception):
System.InvalidOperationException: "The installation failed, and the rollback has been performed."
Inner Exception: "The specified service already exists"
我的直觉是 ManagedInstallerClass 保留了一些东西,因此在同一进程中执行第二次调用时出现了问题。
有人知道发生了什么以及为什么吗?
最佳答案
经过大量测试,我仍然无法解决 ManagedInstallerClass 的问题。
我得到的是问题的解决方法。
所以,而不是调用:
ManagedInstallerClass.InstallHelper(arguments.ToArray());
现在我打电话
callInstallUtil(arguments.ToArray());
函数定义:
public static string InstallUtilPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
private bool callInstallUtil(string[] installUtilArguments)
{
Process proc = new Process();
proc.StartInfo.FileName = Path.Combine(InstallUtilPath, "installutil.exe");
proc.StartInfo.Arguments = String.Join(" ", installUtilArguments);
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
string outputResult = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
// ---check result---
if (proc.ExitCode != 0)
{
Errors.Add(String.Format("InstallUtil error -- code {0}", proc.ExitCode));
return false;
}
return true;
}
针对不同服务多次调用此函数不会产生任何错误,所以我想这对我有用:) - 它不像 ManagedInstallerClass 调用那样优雅,但它完成了工作。
关于c# - 使用 ManagedInstallerClass.InstallHelper 安装多个服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3398701/