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

.net Core how to create services using Worker Service

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces. NET Core how to use Worker Service to create services, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Source of demand

A few years ago, the project was to rely on Windows services to support some business, and now the entire technology stack has been upgraded from .NET Framework to .NET Core x.

Most of the old requirements are to access some third-party hardware devices, such as PLC, serial card reader, TCP reader, various hardware modules.

In order to enable the access of any development language without re-implementation, Windows services are added to support all kinds of hardware API, and external MQTT.NET is used for protocol development.

Version update

Considering the current software updates, including installation files and SDK, which are based on .NET Core, Worker Service is used to create a service that supports running on Windows and Linux.

Create WorkerService

After creation, the entire default project contains Program and an example of Worker, configured in the same mode as ASP.NET Core.

Public class Program {public static void Main (string [] args) {CreateHostBuilder (args). Build (). Run ();} public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .UseWindows Service () .ConfigureServices ((hostContext, services) = > {Windows () });}

You can see that in the configuration service, AddHostedService adds a Worker, the type constraint IHostedService

About the implementation of Worker class

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);}

ExecuteAsync

When the service starts, the method is executed, entering the current time per second

Looking at the implementation of the parent class BackgroundService, you can see that there are methods that support rewriting.

Public virtual void Dispose (); public virtual Task StartAsync (CancellationToken cancellationToken); public virtual Task StopAsync (CancellationToken cancellationToken)

It means that the logic that we can trigger when we start the service and the logic that can be triggered by pausing the service can be written by ourselves.

Installation service

Once released, you can use sc.exe to install / start / pause / uninstall services, and so on.

Installation service

Sc.exe create MyService binPath=C:\ User\ WorkerService.exe

Query service status

Sc.exe query MyService

Start the service

Sc.exe start MyService

Temporarily Out of Service

Sc.exe stop MyService

Uninstall the service

Sc.exe delete MyService

Thank you for reading this article carefully. I hope the article ".NET Core how to create Services using Worker Service" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report