c# - 使用 ManagedInstallerClass.InstallHelper 安装多个服务

标签 c# service installation

我目前正在尝试使用 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/

相关文章:

IXmlSerializable 的 C# XML 序列化类型

java - 为企业 Java 与 C++ 创建 Web 服务应用程序?

javascript - 使用 AngularJS 将变量传递给服务?

python - 有没有办法在同一个conda环境中安装python 3和python 2?

c# - 使用 xml 元素创建 kml 文件

javascript - C# 属性值不会在 Javascript/jquery 中被修改

c# - 无法创建相对 URI,因为 'uriString' 参数表示绝对 URI

android - 如何在服务中显示 GIF?

sql - 如何通过安装程序部署复杂的 SQL 解决方案?

c# - VS 安装项目 - .Net Framework 3.5 或更高版本的启动条件