开发者

Using ManagedInstallerClass.InstallHelper to install multiple services

I am currently trying to install multiple services using ManagedInstallerClass.InstallHelper.

The code execution goes smoothly when I install the first service, then gives an error stating that a service with a same name already exists. However, if I exit the program then execute the same 开发者_如何学Pythonprocess starting from the second service, everything goes smoothly.

The function I use is this one.

ManagedInstallerClass.InstallHelper(arguments.ToArray());

I am 100% sure that the arguments are correct.

The exception I have is this one:

System.InvalidOperationException: "The installation failed, and the rollback has been performed."
Inner Exception: "The specified service already exists"

My gut feeling is that the ManagedInstallerClass keeps something in its belly and thus when executing the second call in the same process something goes wrong.

Anyone has an idea of what's happening and why?


After a lot of testing, I still wasn't able to solve the issue with ManagedInstallerClass.

What I got instead is a workaround of the problem.

So, instead of calling:

ManagedInstallerClass.InstallHelper(arguments.ToArray());

now I call

callInstallUtil(arguments.ToArray());

the function is defined:

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;
} 

Multiple calls of this function for different services yield no errors, so I guess this works for me :) - it's not as elegant as the ManagedInstallerClass call, but it gets the job done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜