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.
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!
- Solved: Build Errors Not Showning in VS 2022 - 21st November 2024
- How To: Configure VSCode to Trust Self-Signed Certs - 16th August 2024
- Solved: GitHub Actions – HTTP 403: Resource not accessible by integration - 13th June 2024
15th June 2018 at 12:05 pm
honestly speaking, this is the article i was googling around for. I am going back to my code to resume my work! nice article.
19th June 2018 at 6:14 am
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.