I would like to create a windows service, using Topshelf, where the runtime configuration is driven by the App.config, and a command line argument.
Stated differently, I'd like the config file (App.config) to contain all the possible configurations, and then have the service select the configuration to use at runtime based on an argument - either "instance", or a custom argument.
What I've put together works when run as a console app, but doesn't work as a service. When run as a service, with...
serviceExe.exe intall -group:test
...the code below correctly sets things like displayName and description. What doesn't happen is configGroup being set to non-null when passed into the constructor during ConstructUsing:
private static int Main()
{
var msghHdlrs = ConfigurationManager.GetSection("domainMessageHandlers") as DomainMessageHandlersSection;
string serviceName = "BDService";
DomainEventHandlerGroup configGroup = null;
var exitCode = HostFactory.Run(host =>
{
host.AddCommandLineDefinition("group", g => {
configGroup = msghHdlrs.Groups.OfType<DomainEventHandlerGroup>().Single(group => group.Name == g);
});
host.ApplyCommandLine();
host.Service<DomainEventSubscriberServiceAdapterCollection>(svc =>
{
svc.ConstructUsing(() =>
{
return new DomainEventSubscriberServiceAdapterCollection(configGroup);
});
svc.WhenStarted(app =>
{
app.StartAll();
}).BeforeStartingService(t => t.RequestAdditionalTime(TimeSpan.FromSeconds(10)));
svc.WhenStopped(app =>
{
app.StopAll();
});
});
host.SetServiceName(serviceName);
host.SetInstanceName(configGroup.Name);
host.SetDisplayName(configGroup.DisplayName);
host.SetDescription(configGroup.Description);
host.RunAsNetworkService();
});
return (int)exitCode;
}
Bottom-line, it seems configGroup gets set for the purposes of things outside of host.Service, but not within. Why is that?
More importantly, is what I'm trying to do possible? Have I missed something?
EDIT
It appears I have the same configuration as Single command line parameter to control Topshelf windows service
and yet it's not working for me...