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
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 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
- https://support.microsoft.com/en-us/help/251192/how-to-create-a-windows-service-by-using-sc-exe
- https://docs.microsoft.com/en-us/dotnet/framework/tools/installutil-exe-installer-tool
- https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490995(v=technet.10)
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.
Comments for this article