I want to pass some custom arguments to the console app when I install and start it as a Windows Service via TopShelf.
When I use:
MyService install start /fooBar: Test
Console application fails:
[Failure] Command Line An unknown command-line option was found: DEFINE: fooBar = Test
Question:
How can I make my arguments to be recognizable by TopShelf so that I can consume their values?
EDIT: This only works when running the .exe, not when running as a service. As an alternative you could add the option as a configuration value and read it at start-up (which is probably better practice anyway):
using System.Configuration;
// snip
string foobar = null;
HostFactory.Run(configurator =>
{
foobar = ConfigurationManager.AppSettings["foobar"];
// do something with fooBar
configurator.Service<ServiceClass>(settings =>
{
settings.ConstructUsing(s => GetInstance<ServiceClass>());
settings.WhenStarted(s => s.Start());
settings.WhenStopped(s => s.Stop());
});
configurator.RunAsLocalService();
configurator.SetServiceName("ServiceName");
configurator.SetDisplayName("DisplayName");
configurator.SetDescription("Description");
configurator.StartAutomatically();
});
According to the documentation you need to specify the commands in this pattern:
-foobar:Test
You also need to add the definition in your service configuration:
string fooBar = null;
HostFactory.Run(configurator =>
{
configurator.AddCommandLineDefinition("fooBar", f=> { fooBar = f; });
configurator.ApplyCommandLine();
// do something with fooBar
configurator.Service<ServiceClass>(settings =>
{
settings.ConstructUsing(s => GetInstance<ServiceClass>());
settings.WhenStarted(s => s.Start());
settings.WhenStopped(s => s.Stop());
});
configurator.RunAsLocalService();
configurator.SetServiceName("ServiceName");
configurator.SetDisplayName("DisplayName");
configurator.SetDescription("Description");
configurator.StartAutomatically();
});