I have written about Topshelf several times before, but the API has gone through a few changes over the last couple of years – and since my Topshelf posts are, for some reason, constantly (according to my stats) being (re)visited, I thought it would be in order to show the most current version of Topshelf in action…
Oh, and if you’re reading this and you don’t know what Topshelf is – it’s a library that helps you to quickly make your C# console application into a Windows Service that can easily be F5-debugged as well as installed as a proper Windows Service.
This is how you do it:
Create a new console application
Just do it. Name it something cool, like
RobotsOnFire.
Pull in Topshelf with NuGet
Either reach out for your mouse and clickety click a few dozen times… me? I prefer to go
|
install-package Topshelf -ProjectName RobotsOnFire |
in the Package Manager Console, and this is how I avoid getting a tennis elbow even before I’ve written one line of code.
Remove unnecessary
using directives and other redundant crap
This looks stupid – it makes you look like a person who doesn’t care:
This looks good – it reaks of the finicky meticulousness that is you:
Now, modify your
Program class
into something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class Program : ServiceControl { static void Main() { HostFactory.Run(f => f.Service<Program>()); } public bool Start(HostControl hostControl) { return true; } public bool Stop(HostControl hostControl) { return true; } } |
and then press F5, and then you’ll be looking at something like this:
Wow! That was easy, right?! In order to actually do something in your Windows Service, you start a thread or a timer or something similar in the
Start method and make sure that what you started is stopped again in the
Stop method. In other words, the
Start and
Stop methods must not block – otherwise, Windows Service Control will not start the service properly.
Since this bad boy is an ordinary Console Application on the surface, it can be F5-debugged and run by executing
RobotsOnFire.exe. But in addition to that, you can do this (in an elevated command prompt):
which results in Robots On Fire turning up as a Windows Service in Windows:
Now, let’s uninstall it again:
and then let’s customize a couple of settings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class Program : ServiceControl { static void Main() { HostFactory.Run(f => { f.Service<Program>(); f.SetServiceName("ServiceName:RobotsOnFire"); f.SetDisplayName("DisplayName:RobotsOnFire"); f.SetDescription("This is my Robots On Fire Windows Service"); }); } public bool Start(HostControl hostControl) { return true; } public bool Stop(HostControl hostControl) { return true; } } |
which, after installing it again, will show up like this:
Topshelf has many additional things that can be customized – e.g. you can perform actions in connection with installing/uninstalling the service, make it install to be run under various system accounts, and – this is one of the cooler things – you can declare a dependency on e.g. a local SQL Server, a local IIS, or any other locally running Windows Service – this way, services will always be started/stopped in the right order, ensuring that e.g. your MSMQ-dependent service is not started before the Message Queueing service.