Windows service - how to set up a name

I wrote a Windows service in C #. The requirement at the beginning was that I should only run one instance of this service, but this has changed, and now I need several instances. This is why I need to change the service name according to the configuration file.

What would be the best way to get him to register with the correct name? Should I write another tool for this? Can I just read the name from the App.config file and set it accordingly in the service and installer classes?

PS> I really don’t understand how this work with names: you need to set the names in the classes of the service and the installer, but then when installing using installutil.exe or even with the new version of powershell, the name should also be specified. Should it be the same thing? Or does one override the other?

+7
source share
4 answers

You can simply read it from app.config and install it in the installer classes.
Typically, a class that inherits from Installer is automatically created. It contains a member of type System.ServiceProcess.ServiceInstaller , most likely named serviceProcessInstaller1 . This is a ServiceName property that you can set. In addition, you need to set the ServiceName property of the ServiceName class to the same value.
In the default implementation, they are set to constant values ​​in the corresponding InitializeComponent methods, but there is no reason to adhere to this. This can be done dynamically without problems.

+4
source

Although I would add my 2 cents since I came across this. I have a file called "ProjectInstaller.cs" with a designer and resources under it. Opening it in a design shows MyServiceInstaller and MyProjectInstaller as elements on the design surface. I managed to change the names in the ProjectInstaller() constructor and manually load the configuration file from the module directory:

 public ProjectInstaller() { InitializeComponent(); var config = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location); if (config.AppSettings.Settings["ServiceName"] != null) { this.MyServiceInstaller.ServiceName = config.AppSettings.Settings["ServiceName"].Value; } if (config.AppSettings.Settings["DisplayName"] != null) { this.MyServiceInstaller.DisplayName = config.AppSettings.Settings["DisplayName"].Value; } } 
+2
source

In the same vein as Jason Goemaat, answer, here is how you would skip all the available installers in your project, which saves you time when you add each new service to this class. In my project, I have a total of 12 services (and we add a new one here and there), and we wanted them to be grouped by instance name, so SVC (instance 1) Service XX and SVC (instance 1) Service YY were next to each other when viewed in the services snap-in console.

  public ProjectInstaller() { InitializeComponent(); var config = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location); string instanceName = config.AppSettings.Settings["Installer_NamedInstanceName"].Value; string instanceID = config.AppSettings.Settings["Installer_NamedInstanceID"].Value; bool usesNamedInstance = !string.IsNullOrWhiteSpace(instanceName) && !string.IsNullOrWhiteSpace(instanceID); if (usesNamedInstance) { foreach (var installer in this.Installers) { if (installer is ServiceInstaller) { var ins = (ServiceInstaller)installer; ins.ServiceName = ins.ServiceName + "-" + instanceID; // Want the service to be named SVC (Instance Name) Audit Log Blah Blah Service ins.DisplayName = ins.DisplayName.Replace("SVC ", "SVC (" + instanceName + ") "); } } } } 

HOWEVER , you need to do something else - when you initialize the service, you must also change the name of the service, otherwise you will receive an error message in the line “Executable file“ Add the service. ”I did this by executing the following code in Program.cs :

  internal static void HandleCustomServiceName(ServiceBase sbase) { if (!string.IsNullOrWhiteSpace(customInstanceName)) { sbase.ServiceName = sbase.ServiceName + "-" + customInstanceName; } } 

Then in the constructor of each service:

  public SystemEventWatcher() { InitializeComponent(); Program.HandleCustomServiceName(this); } 

I supported Jason's answer to pave the way for implementing this in my own project. Thanks!

0
source

Thanks to Jason Gomaat for posting your answer, I could neither comment on nor properly declare your answer because I do not have the necessary reputation (Sigh ...).

0
source

All Articles