Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using InstallUtil to install a Windows service with startup parameters

I am using InstallUtil to install my service and I just cannot figure out how to specify the startup parameters for it!

Here is my Installer subclass:

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

Can anyone indicate how do I specify the service startup parameters?

like image 627
mark Avatar asked Feb 01 '11 12:02

mark


People also ask

What does InstallUtil exe do?

Installutil.exe performs installation in a transactional manner; that is, if one of the assemblies fails to install, it rolls back the installations of all other assemblies.


1 Answers

Found it.

I have rewritten the Install method like so:

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

Although, I give the predefined command line parameters (--service), the code is easily adaptable to pass real command line arguments, just use the same pattern for passing the username and password parameters.

like image 162
mark Avatar answered Oct 01 '22 13:10

mark