【问题标题】:Using InstallUtil to install a Windows service with startup parameters使用 InstallUtil 安装带有启动参数的 Windows 服务
【发布时间】:2011-06-19 06:06:06
【问题描述】:

我正在使用 InstallUtil 安装我的服务,但我不知道如何为其指定启动参数!

这是我的安装程序子类:

[RunInstaller(true)]
public class ServerHostInstaller : Installer
{
  private ServiceInstaller m_serviceInstaller;
  private ServiceProcessInstaller m_serviceProcessInstaller;
  private static string s_usage = "Usage:\ninstallutil /i /username=<user_name> /password=<user_password> NCStub.Server.Host.exe";

  public ServerHostInstaller()
  {
    m_serviceInstaller = new ServiceInstaller();
    m_serviceInstaller.ServiceName = Program.ServiceName;
    m_serviceInstaller.DisplayName = Program.ServiceName;
    m_serviceInstaller.StartType = ServiceStartMode.Automatic;

    m_serviceProcessInstaller = new ServiceProcessInstaller();
    m_serviceProcessInstaller.Account = ServiceAccount.User;

    Installers.Add(m_serviceInstaller);
    Installers.Add(m_serviceProcessInstaller);
  }

  public override void Install(IDictionary stateSaver)
  {
    base.Install(stateSaver);

    string userName = this.Context.Parameters["username"];
    if (userName == null)
    {
      Console.WriteLine(s_usage);
      throw new InstallException("Missing parameter 'username'");
    }

    string userPass = this.Context.Parameters["password"];
    if (userPass == null)
    {
      Console.WriteLine(s_usage);
      throw new InstallException("Missing parameter 'password'");
    }

    m_serviceProcessInstaller.Username = userName;
    m_serviceProcessInstaller.Password = userPass;
  }
}

谁能指出我如何指定服务启动参数?

【问题讨论】:

    标签: c# .net windows-services installutil


    【解决方案1】:

    找到了。

    我已经像这样重写了 Install 方法:

    public override void Install(IDictionary stateSaver)
    {
      string userName = this.Context.Parameters["username"];
      if (userName == null)
      {
        Console.WriteLine(s_usage);
        throw new InstallException("Missing parameter 'username'");
      }
    
      string userPass = this.Context.Parameters["password"];
      if (userPass == null)
      {
        Console.WriteLine(s_usage);
        throw new InstallException("Missing parameter 'password'");
      }
    
      m_serviceProcessInstaller.Username = userName;
      m_serviceProcessInstaller.Password = userPass;
    
      var path = new StringBuilder(Context.Parameters["assemblypath"]);
      if (path[0] != '"')
      {
        path.Insert(0, '"');
        path.Append('"');
      }
      path.Append(" --service");
      Context.Parameters["assemblypath"] = path.ToString();
      base.Install(stateSaver);
    }
    

    虽然我给出了预定义的命令行参数(--service),但代码很容易适应传递真正的命令行参数,只需使用相同的模式来传递用户名 和 密码 参数。

    【讨论】:

    • 如果您将处理程序附加到服务安装程序对象的 BeforeInstall 事件而不是覆盖 Install 方法,则此方法也有效。
    • 其实没有。应该是这样,我很确定以前是这样,但我刚刚检查过,但没有。坚持使用覆盖版本。
    • 我有相同的解决方案将凭据传递给我的安装程序。问题是日志文件也包含您的凭据,我认为这是一个大问题。您知道如何在日志文件中禁用“受影响的参数是:”的写入吗?我不想禁用完整的日志文件!
    • @FlorianGerhardt - 不。从那以后我转向 Java 和 Javascript,过去半年没有 .NET。对不起,伙计。
    • 此解决方案不起作用。它为服务生成以下命令:""C:\folder\file.exe" --service"
    【解决方案2】:

    我知道这是一篇旧帖子,但我想我会发布我的回复。我使用 BeforeInstall 事件在 .net 4 服务中执行此操作。

    ServiceProcessInstaller 的 BeforeInstall 事件:

    private void serviceProcessInstaller1_BeforeInstall(object sender, InstallEventArgs e)
    {
        System.ServiceProcess.ServiceProcessInstaller installer = sender as System.ServiceProcess.ServiceProcessInstaller;
    
        if (installer != null)
        {
            //Get the existing assembly path parameter
            StringBuilder sbPathWIthParams = new StringBuilder(installer.Context.Parameters["assemblypath"]);
    
            //Wrap the existing path in quotes if it isn't already
            if (!sbPathWIthParams[0].Equals("\""))
            {
                sbPathWIthParams.Insert(0, "\"");
                sbPathWIthParams.Append("\"");
            }
    
            //Add desired parameters
            sbPathWIthParams.Append(" test");
    
            //Set the new path
            installer.Context.Parameters["assemblypath"] = sbPathWIthParams.ToString();
        }
    }
    

    安装的服务如下所示:

    它执行得很好,我可以从服务的主函数中检查参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 2013-10-24
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2018-10-12
      相关资源
      最近更新 更多