Thursday, April 26, 2012

How can you create Multiple instances of same windows service

Many times you want to create windows service with multiple instances in the same server. We must create windows service with different name for each instance.

You can add parameters for different configuration of windows service when you try to install.

Here is an example of installing windows service:
installutil -i /name=LinkDevSch2 /account=localsystem LinkDev.Timer.Service.exe
Here is an example of uninstalling windows service:
installutil /u /name=LinkDevSch1  LinkDev.Timer.Service.exe

You can use these parameters to modify server name, way of authentication. the ProjectInstaller has 4 methods you can override OnBeforeInstall, OnBeforeunInstall, Install, and Uninstall which enables us to make changes to the service installer at the at run time.

  
     public string GetContextParameter(string key)
     {
            string sValue = "";
            try
            {
                sValue = this.Context.Parameters[key].ToString();
            }
            catch
            {
                sValue = "";
            }
            return sValue;
     }


      protected override void OnBeforeInstall(IDictionary savedState)
      {
          
            base.OnBeforeInstall(savedState);

            bool isUserAccount = false;
          
            // Decode the command line switches
            string name        = GetContextParameter("name");
            serviceInstaller.ServiceName = name;

            // What type of credentials to use to run the service
            // The default is User
            string acct        = GetContextParameter("account");
          
            if (0 == acct.Length) acct = "user";

            // Decode the type of account to use
            switch (acct)
            {
                case "user":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
                    isUserAccount = true;
                    break;
                case "localservice":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
                    break;
                case "localsystem":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                    break;
                case "networkservice":
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.NetworkService;
                    break;
                default:
                    processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
                    isUserAccount = true;
                    break;
            }

            // User name and password
            string username = GetContextParameter("user");
            string password = GetContextParameter("password");

            // Should I use a user account?
            if (isUserAccount)
            {
                // If we need to use a user account, set the user name and password
                processInstaller.Username = username;
                processInstaller.Password = password;
            }
        }


        protected override void OnBeforeUninstall(IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);

            // Set the service name based on the command line
            serviceInstaller.ServiceName = GetContextParameter("name");
        }

Now you can created NET Windows Service with multiple instance any time you want.

No comments: