If you have been in software development long enough, at some point you must have come across a need to have a long running background service. This came in the form of either windows services or daemons (for non windows systems).
With the release of .NET Core 3.0, there is a new type of application template which was introduced to address the need of building cross-platform worker services. This could either be a Windows service or a systemd process that runs on Linux.
You can either create the worker service using the Visual studio template as below
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidUcU9q324fSYkHOSVy_8oa0VLJI4LMom2oHM9CYOIXZ_KSl21qQltfegioUFqDRyYLrRDgFyFoskfLV8nP5OSSayLJsimj3U3PM11D9AyIUIFSuePhEmFbkpTjeWIozgczepFtD_9vSVH/s640/WorkerCode.png)
The code in ExecuteAsync method logs information into the console every-time the method is invoked. In this case the worker executes every 1 sec (1000 milliseconds) because of the Task.Delay async call. The method also accepts a CancellationToken that allows cooperative cancellation between threads, thread pool work items, or Task objects.
In addition to this there are several other methods which we can override to run a piece of logic at different stages of the worker life-cycle. In order to demonstrate this, I have added the StartAsync and StopAsync methods that run when the host is ready to start or stop the service.
If we now run the application, messages are logged to the console continuously based on the delay interval, as below.
Run as Windows Service
To run the project as a windows service, we need to add the Microsoft.Extensions.Hosting.WindowsServices package to our project. This can either be done by right-clicking on the project and "Manage NuGet Packages..." or by running the below command using the package manager console window.
Install-Package Microsoft.Extensions.Hosting.WindowsServices
Once the package is installed, Add the UseWindowsService() call to the host builder.
dotnet publish -o c:\repo\workerpub
Next use the sc utility in an admin console prompt to run the below command.
sc create workertest binPath=c:\repo\workerpub\Worker.exe
Run as Linux Daemon
Add package Microsoft.Extensions.Hosting.Systemd. Then add User UseSystemd() call to the host builder.
As you can see it's pretty easy to create a create and run a worker service. Also the advantage is that the same code base can be used to run as a windows service or a daemon.
In addition to all of this .NET core worker service comes with built in Logging, Dependency Injection and Configuration support.