Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Worker Services by .NET Core3.0

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to create Worker Services in .NET Core3.0. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

.net CORE 3.0 adds a new project template for Worker Services, which can write long-running background services and can be easily deployed as windows services or linux daemons. If the Chinese version of vs2019 is installed, Worker Services becomes a secondary role service. Worker Services, I don't know how to translate this name, and I don't dare to translate it indiscriminately. I'll keep the original name below. This article will demonstrate how to create a Worker Services project and deploy it to run as a windows service or linux daemon

Start creating a worker service project create a new project-- "Select a secondary role service"

After the project is created successfully, you will see that two classes have been created: 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 ();}); the Program class is very similar to the ASP.NET Core Web application, except that there is no startup class, and the worker service is added to DI container.

Worker.cs

Public class Worker: BackgroundService {private readonly ILogger _ logger

Public Worker (ILogger logger) {_ logger = logger;}

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 a simple class that inherits from BackgroundService, which in turn implements the IHostedService interface.

The default worker demo does not cycle through the print run time every 1 second.

Deploy to run 1. 0 for the Windows service. Add a nuget package to the project: Microsoft.Extensions.Hosting.WindowsServices

two。 Then inside the program.cs, add UseWindowsService () to the CreateHostBuilder

Public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .UseWindows Service () .ConfigureServices ((hostContext, services) = > {services.AddHostedService ();}); 3. Execute the command to publish the project dotnet publish-c Release-o C:\ WorkerPub

4. Then use the sc.exe tool to manage the service, enter the following command to create a windows service sc.exe create DemoWorkService binPath=C:\ WorkerPub\ WorkerService1.exe

Check the status of the service and use the command

Sc.exe query DemoWorkService

Start command

Sc.exe start DemoWorkService

In the list of services, DemoWorkService has been installed successfully

Deactivate, delete command

Sc.exe stop DemoWorkServicesc.exe delete DemoWorkService deployment runs as a Linux daemon

Deploying the linux daemon is also convenient to perform the following two steps:

Add the Microsoft.Extensions.Hosting.Systemd NuGet package to the project and tell your new Worker that its life cycle is managed by systemd!

Add UseSystemd () to the main institution builder.

Public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .UseSystemd () .ConfigureServices ((hostContext, services) = > {services.AddHostedService ();}); this is enough about how .NET Core3.0 creates Worker Services. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report