Using InstallUtil to Install a Windows Service with Startup Options

I use InstallUtil to install my service, and I just can't figure out how to specify startup options 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 someone please indicate how to specify the startup parameters of the service?

+13
c # windows-services installutil
01 feb. '11 at 12:29
source share
2 answers

Found.

I rewrote the installation method as follows:

 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 predefined command line options ( - service ), the code easily adapts to pass real command line arguments, just use the same template to pass username and password .

+15
01 feb. '11 at 13:57
source share

I know this is an old post, but I thought I would post my answer. I did this in a .net 4 service using the BeforeInstall event.

ServiceProcessInstaller BeforeInstall event:

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

The installed service is as follows: enter image description here

It works fine, and I can check the parameters from the main function of the service.

+3
Sep 17 '15 at 20:07
source share



All Articles