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?
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.