Published by breki on 29 Feb 2008 at 12:57 pm
Multiple instance Windows service in C#
The standard way of installing Windows services using the Visual Studio Windows Service project and the InstallUtil.exe tool does not allow the user to specify the service name after the project has been compiled. This is problematic if you want to install two or more instances of a Windows service on the same computer. So in this post I propose a way to achieve this without much effort.
The procedure consists of the steps described below. I’ll assume that you already have a Visual Studio project and you want to extend it with the multiple instance support:
- Add a new XML file named App.InstallConfig.xml to your project. It should contain the following XML code:
<servicename>My Service Name</servicename>
</installconfig>
- Add a post-build step to the project:
copy “$(ProjectDir)App.InstallConfig.xml” “$(TargetPath).InstallConfig.xml”
This will copy the installation configuration file from the project directory to the target and will replace the ‘App’ name with the name of your project’s assembly.
- Add a new private method to the main (project) installer class:
{
// find the install configuration file
string assemblyLocation = Assembly.GetExecutingAssembly ().Location;
string installConfigFile = assemblyLocation + ".InstallConfig.xml";
// now load the file and parse it
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load (installConfigFile);
XmlNode serviceNameNode = xmlDoc.SelectSingleNode ("InstallConfig/ServiceName");
if (serviceNameNode != null)
{
string serviceName = serviceNameNode.InnerText.Trim ();
this.ListenerServiceInstaller.ServiceName = serviceName;
}
}
This method opens the mentioned configuration file and reads the InstallConfig/ServiceName setting which should contain the actual name of the Windows service instance. It uses this name to set the ServiceInstaller.ServiceName before the installation is initiated.
- And finally, add the call of the ConfigureInstaller() method to the project installer constructor, like this:
{
// This call is required by the Designer.
InitializeComponent();
ConfigureInstaller ();
}
NOTE: replace the name of the ProjectInstaller constructor with your own class name.
This should be it. To install the service under the desired name, you just edit the .InstallConfig.xml file before calling the InstallUtil tool.
Extending this further
You can use the same system to include some other Windows service parameters (like the user account under which it will run, for example). The installer class provided by the .NET Framework does not allow setting the Windows service’s description, but there is a way to do this (see the “Adding a description to a .NET Windows Service” article on CodeProject). Then you can add the description tag in the .InstallConfig.xml file and have a different description for each of the service instances.
Also check out the “Windows Services Can Install Themselves” article on how to create self-installing Windows services (be sure to read the article’s comments, they contain some additional tips).


sri on 03 Apr 2008 at 4:58 #
cool
Mike Scullion on 25 Jul 2008 at 10:42 #
If you’re encountering the “command exited with code 1″ or “The command copy exited with code 1″ change the line:
copy $(ProjectDir)App.InstallConfig.xml $(TargetPath).InstallConfig.xml
to become
copy “$(ProjectDir)App.InstallConfig.xml” “$(TargetPath).InstallConfig.xml”
The problem is the space characters in the folder structure, so we simply enclose them with quotes, and that should fix the problem
Suresh on 09 Oct 2008 at 20:41 #
nice dude. Thanks
Rend on 04 Mar 2009 at 14:06 #
Good article.
I have the problem with uninstalling of this windows process. Service name is remain in services console after uninstalling process is completed, so this prevent from installing it another time. According to event log uninstalling process is successfully completed and there is no entry in “Add/Remove Programs” and service directory is cleaned, but service name is not removed from services console.
Any suggestions?
Thanks.
breki on 05 Mar 2009 at 6:18 #
@Rend,
What is the state of the service in the services console after the uninstall? Is it marked as “Disabled”?
Rend on 05 Mar 2009 at 9:43 #
Hi breki,
The service state is remains “Stoped”. Command prompt : “sc delete ” command can be used to remove service from services console(found in http://stackoverflow.com/questions/197876/how-do-i-uninstall-a-windows-service-if-the-files-does-not-exist-anymore). In my opinion, it’s still bypass but not solution.
Do you have the same problem?
Thanks.
breki on 05 Mar 2009 at 15:21 #
Well I haven’t really encountered the uninstall problem, but I did notice that installutil refers to the service with its original name (not the one given programmatically), so I guess there’s a “hole” in the this system. Maybe some additional uninstall code is needed, I need to investigate this further.
Thanks for letting me know about the bypass solution.