Monday, January 21, 2008

Dynamic Services

Something useful I recently learned:

While you can without any problem attach parameters to a .NET Windows Service (by means of a configuration file), how the heck am I supposed to enter parameters into the INSTALL program itself (such as, the service name...).

The answer was simple: InstallUtil.exe (the installer program) can actually be called with command-line parameters appended to it. These can then easily be processed in the installer class itself.

installutil /DisplayName=Sam " WindowsService1.exe"


And then in your installer.cs file:

protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
string name = Context.Parameters["DisplayName"];
if (name != null)
serviceInstaller1.ServiceName = name;
}


Take care: this needs to happen in your OnBeforeInstall handler, after that your Context will be NULL!

No comments: