Installing same Windows service under a different name

How to install same Windows service with different name on the same host

Microsoft .NET framework comes with a really good support for Windows services. Regarding the project itself, Visual Studio IDE comes with build in project template for Windows service.

.NET framework itself comes with a built in tool installutil for deploying the Windows Service project. It relies on the System.Configuration.Install.Installer class which you can add to your project and just call insallutil to setup your Windows Service.

    [System.ComponentModel.RunInstaller(true)]
    public partial class MyServiceInstaller : System.Configuration.Install.Installer
    {
        public MyServiceInstaller()
        {
            var processInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            var serviceInstaller = new System.ServiceProcess.ServiceInstaller();

            processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            serviceInstaller.DelayedAutoStart = false;

            serviceInstaller.DisplayName = "MyServiceDisplayName";
            serviceInstaller.ServiceName = "MyServiceName";
            serviceInstaller.Description = "MyService description text";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
    

Using installutil for a service with implemented service installer is easy and with one single line and path to service you can install the service with all the properties defined in the service installer class 

%windir%\Microsoft.NET\Framework64\v4.0.30319\installutil C:\services\MyService\MyService.exe

Using same service with different configuration is one of the scenario you might run into. The is easily done with different build configuration transformations.

In case you need the same service running on the same host but with different configuration, logically you would use same code just copy the service folder with different configuration and use installutil to install service with a different name. Unfortunately this will not work, well at least the last part with creating the service with a different name.

This can be done with sc tool which is build in in Windows operating system. All the settings defined with ServiceInstaller class set be also set through parameters of sc command line tool as following 

sc create MyServiceName1 obj=LocalSystem DisplayName=MyServiceDisplayName1 start=auto binpath=C:\services\MyService1\MyService.exe

SC tool will ignore the ServiceInstaller class implementation and set the values of the parameters set in the tool command call. This way you will have same code base with two different configurations running as two separate services on same Windows host operating system.

 

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan Stojanovic is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform includion ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article