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;
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!
Derreck dean
source share