I was looking at the TopShelf framework for developing Windows Services. I’ll admit at first I was a little underwhelmed. Developing a service is really only a little more complicated then creating a console application. Nine times out of ten, I’ll prototype in a console application anyway and refactor from there. So I was initially not a fan of the framework. However, as I explored more I decided that it is a valuable way to develop Windows services. Some of the things that changed my mind
- Ease of use. Like I said, I like to protoype in Console applications. That way I can experiment with intersting code, and fit it into whatever framework the project is using after the fact. I find this approach gives me multiple opportunities to refactor and generally ends up with a better design as the console app gets decomposed. Introducing TopShelf was super easy (check out their quickstart) and it leaves the Main method totally concerned with running/installing your service only. All of your logic needs to go in another class.
- Consistency of servicing. Once your service is done, you just need to hand it off to your DevOps team who will use InstallUtil to install the app as a service, and then they may need to tweak logon or recovery options. Maybe they will use the sc command to configure, maybe they do it by hand…. You see where I am going with this. As you add more services and configuration steps get more complex, this can get harder and harder. TopShelf provides a standard cli for all of your services and it supports a help command to let users and admins explore on their own. This part isour very cool and allows the developer to tweak all of the settings exactly as they want the app to run or gives them the flexibility to let an admin configure those settings at runtime.
- Expose as a Console application. This is my very favorite feature. Using TopShelf makes it very easy to expose your services as a Console app as well. This is great for prototyping, one-off requirements, or troubleshooting. Your app is more flexible and you don’t have to write more code. This is a huge win for me and one of the biggest benefits of TopShelf IMHO.
So next time you need a windows service, search for TopShelf on nuget and give it a try