Topshelf OSS Library – an unsung hero for windows services development

Creating a windows service itself is a hassle that includes various aspects of it like configuration, install-uninstall, recovery, start mode etc. Managing this settings is either can be achieved manually or by writing bunch of batch/command files that helps in deployment and management of windows services.
Recently, I came across an ultimate .Net OSS library that provides control almost everything related to a Windows service. The library is known as “TOPSHELF”, the contribution creds of this library goes to Travis Smith, Charles patterson and few more awesome contributors. See the well managed documentation of this project here.

How this library tool makes a difference creating a windows service? Let’s try to create a windows service:

public class TownCrier
    readonly Timer _timer;
    public TownCrier()
        _timer = new Timer(1000) {AutoReset = true};
        _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
    public void Start() { _timer.Start(); }
    public void Stop() { _timer.Stop(); }

The above code is expected to run as windows service. Now conventionally to make it a window service one must add a Window service file type then WindowsInstaller etc. Rather I’ll be using Topshelf library for hassle free setup:

Windows services made easy

public class Program
    public static void Main()
        HostFactory.Run(x =>                                 //1
            x.Service<TownCrier>(s =>                        //2
               s.ConstructUsing(name=> new TownCrier());     //3
               s.WhenStarted(tc => tc.Start());              //4
               s.WhenStopped(tc => tc.Stop());               //5
            x.RunAsLocalSystem();                            //6
            x.SetDescription("Sample Topshelf Host");        //7
            x.SetDisplayName("Stuff");                       //8
            x.SetServiceName("Stuff");                       //9
        });                                                  //10

That’s it now when I run this code my service will run as Console. Without Topshelf I would have create a separate class to run the service as console. But now I can see in few lines of code things are quite awesome. Let’s simply press F5 in visual studio to run the above code:

Running as Console service

Let’s simply press F5 in visual studio to run the above code:


Installation of window service

Simply call the generated .exe file with argument

SampleService.exe install

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

Let’s take a look at the services window. Goto Run –> Services.msc.


And I can see my service is started and running. How cool is it..I’m sure this kinda stuff will make DevOPs guys happy. There are plenty of option that are configurable easily using this library, I can say OOS stack of .Net is coming into picture and helping developers making softwares better an better everyday.

1 comment: