0

I have a console application that is written to take command line arguments which will be used in determining the number of Windows services are needed. The command line for the console application is like this: consoleapp.exe -server:11 -azure:7 where -server specifies a Windows service and -azure specifies an Azure WebJob. [NOTE: This question only pertains to the Windows service but I wanted to show that the console application can potentially have many arguments.] In the console application I parse the command line and, if the command matches "-server" then I want to create a Windows service using TopShelf. I can potentially have multiple -server commands on the console app command line, or single -server commands with multiple values, as in: -server:11,7 or -server:11 -server:7 For each distinct -server/value I am creating a Task that in turn creates and starts a Topshelf service, like so:

                TopshelfExitCode retCode = HostFactory.Run(x =>
        {
            x.Service<TopshelfWindowsService>(sc =>
            {
                sc.ConstructUsing(name => new TopshelfWindowsService(companyConfig, runnerProgress));
                sc.WhenStarted((s, hostControl) => s.Start(hostControl));
                sc.WhenShutdown(s => s.Shutdown());
                sc.WhenStopped((s, hostControl) => s.Stop(hostControl));
            });
            //
            x.SetServiceName($"CommRunner {companyConfig.CompanyName + companyConfig.CompanyId}");
            x.SetDescription($"Runner for CompanyID ({companyConfig.CompanyId})");
            x.SetDisplayName($"Runner {companyConfig.CompanyId}");
            //
            x.StartAutomaticallyDelayed();
        });

My problem is that Topshelf apparently uses the console application's command line arguments during the service configuration and I end up getting an error: "[Failure] Command Line An unknown command-line option was found: DEFINE: server = 11".

Is it possible to do what I am attempting and still use Topshelf? Is there any way to disable the use of the command line when configuring a service in Topshelf?

JNickVA1
  • 418
  • 7
  • 23

1 Answers1

0

I could be wrong, but it sounds like your issue isn't really how to run multiple instances in separate threads, but more how to parse command line arguments of your own with TopShelf in use.

Have a look at the AddCommandLineSwitch functionality to allow you to create and use your own arguments.

x.AddCommandLineSwitch("server", v => server = v);
x.AddCommandLineSwitch("azure", v => azure= v);
x.ApplyCommandLine();

From this the syntax is:

-server:11 -azure:7 

See How can I use CommandLine Arguments that is not recognized by TopShelf? for more information.

Remember, these only work during the install phase. To use these parameters for when the service starts, have a look at: How to specify command line options for service in TopShelf

reckface
  • 5,678
  • 4
  • 36
  • 62