Showing posts with label Windows Service. Show all posts
Showing posts with label Windows Service. Show all posts

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.

Thursday, February 12, 2009

Installing Multiple Instances of Windows Service

To Installing Multiple Instances of Windows Service.

  • Install InstallUtil.exe.
  • Add a /name parameter to specify what the service will be named. The names have to be unique, so if you already have "Telligent Mail Gateway", you might need to name it "Telligent Mail Gateway - Site 2".
  • Specify the filename of the Windows Service, which is usually like "....Service.exe”. With Mail Gateway.

    it would be like this:
    InstallUtil /name="Telligent Mail Gateway - Site 2" Service.Service.exe

    Run this from a command prompt within the add-ons directory. The name must be in quotes if it contains spaces and must come before the filename.
How to Uninstall:

Uninstalling the service is very similiar to installing. You will still use the InstallUtil.exe program and will need to specify the name of the service to uninstall, though this time you will need to add a /u switch to specify that you intend to uninstall it. For instance:

InstallUtil /u /name="Telligent Mail Gateway - Site 2" Service.Service.exe

The name must be specified since if you have multiple instances of the same executable installed, it will need to know which one. If no name is specified, it will remove the one with the default name as opposed to a custom named one.

Again, the uninstall and name parameters must come before the filename so that it know they are for that file.