Saturday, June 13, 2009

Passing Parameter to InstallUtil

When installing window service or any installer ( class inherits from install class) required custom parameter at run time , Need to pass as parameter of installutil.

Crating window service using article (http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html).  This article suggest you add installer ( ProjectInstaller). Now we use installer class events to configure ServiceProcessInstaller and ServiceInstaller as per parameter pass to installutil.

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
           this.BeforeInstall += new InstallEventHandler(ProjectInstaller_BeforeInstall);
            this.BeforeUninstall += new InstallEventHandler(ProjectInstaller_BeforeUninstall);
        }       

        void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e)
        {
            // Configure Account for Service Process.
            switch(this.Context.Parameters["Account"])
            {
                case "LocalService":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
                    break;
                case "LocalSystem":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                    break;
                case "NetworkService":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.NetworkService;
                    break;
                case "User":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
                    this.serviceProcessInstaller1.Username = this.Context.Parameters["UserName"];
                    this.serviceProcessInstaller1.Password = this.Context.Parameters["Password"];
                    break;
            }      
            // Configure ServiceName
            if(!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
            {               
                this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
            }
        }

        void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
            {
                this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
            }
        }
    }

Above ProjectInstaller class handle two events : BeforeInstall and BeforeUninstall. BeforeInstall is called just before install method of installer (ProjectInstaller) called via InstallUtil and same way BeforeUninstall called just before Uninstall method of projectinstaller class. After adding that two event handler use following to configure service using installutil.

1. Install
installutil /Account=User /UserName=admin /Password=admin /ServiceName=WinService1 /i WindowService1.exe

2. Uninstall
installutil /ServiceName=WinService1 /u WindowService1.exe

This will also help you configure WindowService1.exe with two different name and different account.
installutil /Account=User /UserName=admin /Password=admin        /ServiceName=WinService1 /i WindowService1.exe

installutil /Account=LocalSystem /ServiceName=WinService2 /i WindowService1.exe

3 comments:

Anonymous said...

In InitializeComponent() method if i write service name initialization command through context then i am getting error..."Service name can't be empty"

Following is the code snippet for the same. let me know if you could figure out the problem.

this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];

I am using following command to run installer

installutil /ServiceName=Hello assemblyname /ShowCallStack /LogToConsole=true

Please advise...Awaiting for your reply.

Anonymous said...

It works for me. Thank u very much!!!!

Anonymous said...

awesome, exactly what I was looking for. Thanks!