Windows service - how to make name configurable
You can simply read it from the app.config and set it in the installer classes.
Normally, a class that inherits from Installer
is automatically created. It contains a member of type System.ServiceProcess.ServiceInstaller
, most likely named serviceProcessInstaller1
. This has a property ServiceName
you can set. Additionally, you need to set the ServiceName
property of the ServiceBase
derived class to the same value.
In a default implementation, these are set to constant values in the respective InitializeComponent
methods, but there is no reason to stick with this. It can be done dynamically without problems.
I though I'd add my 2 cents since I ran into this. I have a file called "ProjectInstaller.cs" with designer and resources under it. Opening it up in design shows MyServiceInstaller and MyProjectInstaller as items on the design surface. I was able to change the names in the ProjectInstaller()
constructor, and manually loaded the config 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;
}
}