While recently writing a windows service which should be a multi-instance Service, I encountered the following challenge. As in SQL Server I wanted to use a similiar naming convention like MyServiceName$InstanceNameHere to distinguish the services from another. Within the service I needed exactly this service name in order to gather the right configuration. Fortunately, the Service class inherited already a property called ServiceName from ServiceBase, so I thought I was already done with all the hard work
It turned out that this ServiceName is not the one that I assigned to the Service during installation using the ServiceInstaller class, as this will be mapped to different settings in the ServiceController:
Thinking of it, it makes perfect sense as the executable itself doesn´t know which ServiceName was assigned to it during installation using an installer or installutil. On the other hand it is confusing using the same name for different sort of things, why not using simply “Name” in the Service class itself as in other component, or did anyone ever see "FormName" instead of simply "Name" of a Windows.Forms class ?
Looking forward, what can we do to acquire the real service name as in the properties windows of the controller ? The solution is to get the Information of your process id and find the the current process in the ServiceController to query the property as follows:
This will actually get you the ServiceInstaller.ServiceName as pointed out above.