What is Topshelf?
Many of you probably have experienced the pain of debugging Windows Service applications. A nice alternative is Topshelf Windows Services.
Topshelf allows developers to write Windows Services which can operate as regular Console applications. This is a great open source product that deserves more recognition in the community and that's why I decided to write a quick post about it. It makes the overall development experience a much, much better one!
Show Me An Example
Here's a quick example of how easy it is to use Topshelf. Obviously this example doesn't conform to general software development best practices, it is only intended to give you a quickstart overview.
1. Create a new Console application in Visual Studio.
2. Install the Topshelf Nuget package
PM> Install-Package TopShelf
3. Next we'll create a simple class with public START and STOP methods that will later be invoked by the service.
4. Now we'll define the main program. Here we need to wire up the Topshelf service. The START and STOP methods we defined previously will be invoked when the Topshelf service starts and stops.
5. The Windows Service doesn't get created until you invoke the console application we just created with the INSTALL parameter. The Windows Service will be immediately created on your host machine. Note: You'll need elevated administrative privileges.
If you however just want to execute the console application without installing the Windows Service, simply invoke the console application without any input parameters. This is great when you need to debug an issue with a given Windows Service. For those that have dabbled with Windows Service projects, this is a much improved experience!
6. To uninstall the Windows Service you just created, execute the console application with the UNINSTALL parameter.
That's it, that's all there is to it. This is a fantastic open source project so please share it with others. So next time you need to write a Windows Service application, give it a try!