Creation and deployment of a C# Windows Service using Topshelf
You can find my full blog post here (French).
I used to develop Windows Services without the need of any third-party libraries. I created some code used to manage my service (bootstrapper), like the processing, but also the entire lifecycle and even install/uninstall features. That allowed me to easily debug locally. I then added my code, which was concise and robust in production, on the private NuGet of my company for reusing it on all our Windows Services projects.
Topshelf is widely kwnown an used library among C# developers for creating Windows Services, this time I wanted to give it a try. Top-features I like the most:
- Cross-platform: host services on both Windows and Mono
- Easy to use
- Highly Configurable
- Powerfull command line
- .NET Core/Framework compatibility
Yes! As I said in the previous paragraph, you can do the old way by creating a Console application, then managing installation with sc
commands or better by creating your boostrapper.
Better, if you are using .NET Core 3.0+, you can create a Worker project (dotnet new worker
). Worker projects fit better with the .NET Core way as they are similar with ASP.NET Core on how they are boostrapped and you can easily reuse code and integrate them together. However with Worker projects you will have to still use sc
commands.
If you are using Cloud services you may be interested in Azure Functions or AWS Lambda.
md src
cd src
dotnet new sln -n "WinServiceDemo"
dotnet new console -n "WinServiceDemo.Console"
dotnet sln add "WinServiceDemo.Console"
Step 4 - Add Topshelf NuGet package:
cd "WinServiceDemo.Console"
dotnet add package Topshelf --version 4.2.1
- Create ServiceManager.cs
- Add Start() method. This method will be fired whenever the service is started.
- Add Stop() method. This method will be fired whenever the service is stopped.
- Add Process(object sender, ElapsedEventArgs eventArgs) method. This method will be fired on our custom time defined tick.
- Define timer with a custom interval time in OnStart(). Set the timer Elapsed property tou our Process() method. In OnStop() dispose the timer.
In Program.cs, configure the main entrypoint to initialize Topshelf on top of our service manager
Topshelf makes it easy to debug our application, which is basically a simple console app. To test it, just hit "F5" on Visual Studio or execute the dotnet run
command.
- Create the artifact
dotnet build
dotnet publish
- Install
WinServiceDemo.Console.exe install
Get-Service -Name "WinServiceDemo.Console" | Start-Service
Note: I used Powershell command for starting the service because otherwise when calling our .exe the Program Main method will log twice, and if you use file logger your file could already be locked because in use by another process.
- Uninstall
Get-Service -Name "WinServiceDemo.Console" | Stop-Service
WinServiceDemo.Console.exe uninstall
Note: When you uninstall it the OnStop() method is correctly fired and your service manager correctly cleaned. However, I noticed that tis way does not correctly stop it, the service status is marked as deactivated and our .exe is still running in task manager. This problem means that we cannot copy our new version of the application because some files are in use and the locked by the operating system. Stopping the service via Powershell command and then uninstalling it fixed this issue.
You can find in /scripts folder the Powershell files that can be used in your CI/CD pipeline:
- Get your artifact
- Enventualy set environment variables or appsettings.json environment values
- service-stop.ps1: Stop the service. If service missing, command is automatically skipped
- service-uninstall.ps1: Uninstall the service. If .exe missing, command is automatically skipped
- service-wait-locks.ps1: In my case there were very long running threads/tasks making service stop/uninstall quite long and that can reach timeout. To avoid operating system file locks during new version copy the script check when file are not locked (e.g. log4net.dll not in use here) to allow a safe upgrade.
- Copy artifact files on server
- service-install.ps1: Install the service
- service-start.ps1: Start the service
- Enjoy!