.NET CORE 3.0 Worker Services added a new project templates, you can write long-running background service, and can easily be deployed into service windows or linux daemons. If you are installing vs2019 is the Chinese version, Worker Services into a supporting role services. Worker Services we do not know how this translated into the name, Ganluan translation below to keep the original name. . . This paper will demonstrate how to create a Worker Services project, and deploy service run as windows or linux daemons;
Start creating worker service project
Create a new project - "Choose a supporting role service
After the project is successfully created, you will see created two classes: Program and Worker.
Program.cs
public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); });
Program class with ASP.NET Core Web application is very similar, except that there is no startup class, and adds to the DI container service worker in.
Worker.cs
public class Worker : BackgroundService { private readonly ILogger<Worker> _logger; public Worker(ILogger<Worker> logger) { _logger = log; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(1000, stoppingToken); } } }
worker is simply a class that inherits from BackgroundService , which in turn realize IHostedService interface.
The default worker demonstration, not every one second, circulation print run.
Deployed as a Windows Service
1. Add nuget package in the project: Microsoft.Extensions.Hosting.WindowsServices
2. Then within program.cs, the UseWindowsService () added to CreateHostBuilder
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseWindowsService() .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); });
3. Run the following command to publish a project
dotnet publish -c Release -o C:\WorkerPub
4. Then use sc.exe tool to manage the service, type the following command to create a windows service
sc.exe create DemoWorkService binPath=C:\WorkerPub\WorkerService1.exe
Check the service status using the following command
sc.exe query DemoWorkService
Start command
sc.exe start DemoWorkService
In the service list view, DemoWorkService has been successfully installed
Disable, Delete command
sc.exe stop DemoWorkService
sc.exe delete DemoWorkService
Deployment runs as a Linux daemon
Deploy linux daemon is also very easy to look at the implementation of two steps:
- Add Microsoft.Extensions.Hosting.Systemd NuGet package to the project, and tell your new Worker, life cycle management by the systemd!
- The UseSystemd () added to a host builder.
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSystemd() .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); });