Topshelf OSS Library

Creating a Windows Service itself is a hassle that includes various aspects of it such as configuration, install-uninstall, recovery, start mode and so on. Managing this setting either can be done manually or by writing many batch/command files that help in deployment and management of Windows Services.

Recently, I came across an ultimate .NET OSS library that provides control of nearly everything related to a Windows Service. The library is known as “TOPSHELF”, the contribution credit for this library goes to Travis Smith, Charles Patterson and a few more awesome contributors. See the well-managed documentation of this project here.

How does this library tool make a difference creating a Windows Service? Let's try to create a Windows Service as in the following:

  1. public class TownCrier   
  2. {  
  3.     readonly Timer _timer;  
  4.     public TownCrier()   
  5.     {  
  6.         _timer = new Timer(1000)   
  7.         {  
  8.             AutoReset = true  
  9.         };  
  10.         _timer.Elapsed += (sender, eventArgs) = > Console.WriteLine(  
  11.             "It is {0} and all is well", DateTime.Now);  
  12.     }  
  13.     public void Start()   
  14.     {  
  15.         _timer.Start();  
  16.     }  
  17.     public void Stop()   
  18.     {  
  19.         _timer.Stop();  
  20.     }  
  21. }  
The preceding code is expected to run as a Windows Service. Now conventionally, to make it a Windows Service, one must add a Windows Service file type, then WindowsInstaller and so on. Rather I'll be using the Topshelf library for a hassle-free setup.

Windows Services made easy
  1. public class Program   
  2. {  
  3.     public static void Main()   
  4.     {  
  5.         HostFactory.Run(x = > //1  
  6.         {  
  7.             x.Service < TownCrier > (s = > //2  
  8.             {  
  9.                 s.ConstructUsing(name = > new TownCrier()); //3  
  10.                 s.WhenStarted(tc = > tc.Start()); //4  
  11.                 s.WhenStopped(tc = > tc.Stop()); //5  
  12.             });  
  13.             x.RunAsLocalSystem(); //6  
  14.   
  15.             x.SetDescription("Sample Topshelf Host"); //7  
  16.             x.SetDisplayName("Stuff"); //8  
  17.             x.SetServiceName("Stuff"); //9  
  18.         }); //10  
  19.     }  
  20. }  
That's it. Now when I run this code my service will run as a console application. Without Topshelf I would have created a separate class to run the service as a console application. But now I can do it in a few lines of code and that is quite awesome. Let's simply press F5 in Visual Studio to run the preceding code.

Running as Console service

Let's simply press F5 in Visual Studio to run the preceding code.



Installation of Windows Service

Simply call the generated .exe file with an argument.

SampleService.exe install

You will see some success log output on the command prompt:



Let's have a look at the Services window. Go to Run, then Services.msc.



And I can see my service has begun and is running. How cool, isn't it? I'm sure this kinda stuff will make the DevOPs guys happy. There are many configuration options that make use of this library easy. I can say that the OOS stack of .NET is coming into the picture and helping developers making software better and better everyday.


Similar Articles