TopShelf looks interesting, I have written quite a few Windows services over the years and there have always been lots of gaps in the MS offering.
In your Main method:
if (args.Length > 0 && args[0] == "-a")
{
// Running as an application
MyService ms = new MyService();
ms.DoStart();
Console.WriteLine("Press Enter to Stop");
Console.ReadLine();
ms.DoStop();
} else {
// Running as a service
ServiceBase[] servicesToRun = { new MyService() };
ServiceBase.Run(servicesToRun);
}
And in the MyService class that derives from ServiceBase: protected override void OnStart(string[] args)
{
DoStart();
}
protected override void OnStop()
{
DoStop();
}
internal void DoStart()
{
// Start your service thread here
}
internal void DoStop()
{
// Tell your service thread to stop here
} private static void Main()
{
var serviceHandler = new ServiceHandler();
if (Environment.UserInteractive)
{
serviceHandler.OnStart(null);
Console.WriteLine("Service started! Press <ENTER> to terminate service.");
Console.ReadLine();
serviceHandler.OnStop();
Console.WriteLine("Service stopped!");
return;
}
Run(serviceHandler);
}I'm glad to hear you're getting value out of it! Last place I was at, we had about 30 distinct services running. It was a huge bonus to have Topshelf.
TBH, I think you might want to deprecate that whole way of using TopShelf. Using ServiceControl is much much easier and better documented. I think if one wants to keep TopShelf out of their core code, it's easier to create a separate "ServiceWrapper" project that has a ServiceControl subclass and calls into your other code, rather than using the HostControl.Service() style.