Shane Bartholomeusz

Technical Solutions Consultant

Topshelf – Windows Services Done Right

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.

Code Snippet Screenshot

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.

Code Snippet Screenshot

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!

Console Screenshot
Screenshot

6. To uninstall the Windows Service you just created, execute the console application with the UNINSTALL parameter.

Console Screenshot

Final Thoughts

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!

Shane Bartholomeusz
Latest posts by Shane Bartholomeusz (see all)

2 Comments

  1. honestly speaking, this is the article i was googling around for. I am going back to my code to resume my work! nice article.

  2. how to use this for tasks that needs to run every hour or when a file is being dropped to a shared path and ready for use? i notice some forums talks about clean design is not to use timers in the windows service. I can use the FileWatcher but i am looking for or interested to know if there is any better technical and architectural approach for my above use case.

    use case in detail:
    * every hour, there will be X number of files be placed in shared path.
    * I need to read them, process them and archive them so the next hour run will go successfully, if not, it will load the new files rather fail.

Leave a Reply

© 2020 Shane Bartholomeusz

Theme by Anders NorenUp ↑