I have a console .NET application which can be run both from the console and as a windows service (after being registered as such). The process is utilizing the Topshelf .NET library.
In both cases, the entry point is the same - Program.Main, but the command line arguments are different. However, I do not want to base my decision on the command line arguments.
Here is the Topshelf code used by the Main:
var exitCode = HostFactory.Run(configurator =>
{
configurator.UseNLog();
configurator.RunAsLocalSystem();
configurator.SetServiceName(Cfg.ServiceName);
configurator.SetDescription(serviceDescription);
configurator.SetDisplayName(serviceDisplayName);
configurator.EnablePauseAndContinue();
configurator.Service(factory =>
{
var bjeServer = DependencyResolver.Instance.GetInstance<IBJEServer>();
bjeServer.Initialize();
return (ServiceControl)bjeServer;
});
});
if (exitCode != TopshelfExitCode.Ok)
{
Environment.Exit(1);
}
// This may actually not terminate the process right away. The reason - Quartz may still be waiting for jobs to finish, which may take hours.
return 0;
I am OK if the solution is Topshelf specific. A general one is welcome as well.