使用ManagedInstallerClass.InstallHelper安装多个服务

sh0*_*ama 3 c# installation service

我目前正在尝试使用ManagedInstallerClass.InstallHelper安装多个服务.

当我安装第一个服务时,代码执行顺利进行,然后给出一个错误,指出已经存在具有相同名称的服务.但是,如果我退出程序然后从第二个服务开始执行相同的过程,一切顺利.

我使用的功能是这个.

ManagedInstallerClass.InstallHelper(arguments.ToArray());
Run Code Online (Sandbox Code Playgroud)

我100%确定参数是正确的.

我有一个例外就是这个:

System.InvalidOperationException: "The installation failed, and the rollback has been performed."
Inner Exception: "The specified service already exists"
Run Code Online (Sandbox Code Playgroud)

我的直觉是ManagedInstallerClass在它的腹部保留了一些东西,因此当在同一个过程中执行第二次调用时出现问题.

任何人都知道发生了什么以及为什么?

sh0*_*ama 6

经过大量测试后,我仍然无法使用ManagedInstallerClass解决问题.

我得到的是解决问题的方法.

所以,而不是呼吁:

ManagedInstallerClass.InstallHelper(arguments.ToArray());
Run Code Online (Sandbox Code Playgroud)

现在我打电话

callInstallUtil(arguments.ToArray());
Run Code Online (Sandbox Code Playgroud)

该函数定义为:

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;
} 
Run Code Online (Sandbox Code Playgroud)

对不同的服务多次调用此函数不会产生任何错误,所以我想这对我有用:) - 它不像ManagedInstallerClass调用那么优雅,但它可以完成工作.