So I have an app that is composed from an API and a Windows service (wrapped by Topshelf) that is continuously listening for events using RabbitMQ and processes data on demand.
For education and fun, I'm trying to rewrite this into an equivalent setup that would run on .NET Core and unix (e.g. in a docker container on AWS)
What would be the best way to implement something equivalent to a windows service like that (forever-running background process) using .NET Core if I want to keep it cross-platform?
NET Core and . NET 5+, developers who relied on . NET Framework could create Windows Services to perform background tasks or execute long-running processes. This functionality is still available and you can create Worker Services that run as a Windows Service.
Windows services are a special class of programs that are configured to launch and run in the background, usually without any sort of user interface and without needing a user to log in to the PC.
NET Core is the venerable cross-platform development stack for Linux, Mac, and Windows. Up to now, .NET Core really hasn’t had a good story for writing daemons, but with the introduction asynchronous Main methods, GenericHosts, and RunConsoleAsync this is not only possible, but incredibly elegant in its implementation.
The background services that we create in .NET are cross-platform and support the inbuilt .NET features like logging, configuration, dependency injection, etc. For creating background services, we can use the Worker Service Template that is available with both the .NET CLI and Visual Studio.
Depending on how you target your projects, it’s possible to have .NET Core code run on the .NET Framework, Mono and Xamarin platforms, on Windows 8 and Windows Phone, and on the Universal Windows Platform (UWP). To learn more, check out the .NET Platform Standard (bit.ly/1Of6W1r). Finally, .NET Core will be “pay-for-play” and performant.
The UseWindowsService (IHostBuilder) extension method configures the app to work as a Windows Service. The service name is set to ".NET Joke Service". The hosted service is registered, and the HttpClient is registered to the JokeService for dependency injection. For more information on registering services, see Dependency injection in .NET.
Windows service by itself is a console application which conforms to the interface rules and protocols of the Windows Service Control Manager. You can achieve the same on both platforms using .net core console application as a host.It will require to do some extra configuration to make it behave more like a real service / daemon.
E.g. for Linux you can use SystemD. You need to create a SystemD configuration file with something like this first:
[Unit]
Description=daemon service
After=network.target
[Service]
ExecStart=/usr/bin/dotnet $(pwd)/bin/daemonsrv.dll 10000
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
And then configure SystemD to make it aware of your service configuration
# Copy service file to a System location
sudo cp daemonsrv.service /lib/systemd/system
# Reload SystemD and enable the service, so it will restart on reboots
sudo systemctl daemon-reload
sudo systemctl enable daemonsrv
# Start service
sudo systemctl start daemonsrv
# View service status
systemctl status daemonsrv
For windows you should do mostly the same but with a different toolset. You will have to use a third party service manager to avoid a tight windows binding. E.g. you can use NSSM. And here is the nice article with examples about it - .Net Core console application as a Windows Service.
Btw you can still use a normal windows service setup just as a host in a case of a Windows. And write another host for you Unix environments (console app host). Both of them can share the business logic and only the way they react to system events will differ.
Hope it helps.
See Worker Services (.NET Core 3.x):
You can create one from the new Visual Studio 2019 Worker Service project template, or by using the .NET CLI:
dotnet new worker
See also: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With