Cross-platform background service in .NET Core (think windows service/unix daemon)?
Asked Answered
I

2

43

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?

Incorporating answered 27/2, 2017 at 10:27 Comment(2)
Interested in this too.Parrnell
This is a long, long away from anything practical. Getting it ported to Windows is their first priority, a set of patches were committed 3 months ago with the milestone set for v2.1.0. But there is still a bunch of stuff missing that is not going to be easy to replace. There will never be an InstallUtil.exe and support for ServiceInstaller, that can be limped with sc.exe. More critical I'd say is the missing support for EventLog. Pretty hard to create a usable service when it can't do basic stuff like logging startup failure.Bellwether
K
6

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

Kerrikerrie answered 4/12, 2019 at 18:8 Comment(0)
P
21

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.

Linux

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

Windows

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.

Perry answered 1/11, 2017 at 8:19 Comment(4)
Thanks for the explanation. Would it be possible for example to implement a cleanup callback on sudo systemctl stop daemonsrv in a .NET Core console app ?Incorporating
Yeah i believe you should be able to hook into console exit event and perform your cleanup actions from there if necessaryPerry
try this, but there are other implementations as well wintellect.com/creating-a-daemon-with-net-core-part-1 I'm trying to combine all to finally get the true daemon that will run in background upon start like certain linux apps with -daemon flagMagness
PS so far I run .net Core console app using "screen" app on linux linuxize.com/post/how-to-use-linux-screen good enough to setup background running console app but not true daemon that could be run via SystemD (except via hack of running .sh script from systemctl where .sh script has screen command)Magness
K
6

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

Kerrikerrie answered 4/12, 2019 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.