A preliminary study on Worker Service

Connect Asia Data learn, and optimize business database management.
Post Reply
aminaas1575
Posts: 32
Joined: Sun Dec 22, 2024 4:03 am

A preliminary study on Worker Service

Post by aminaas1575 »

Since development has recently been changed to .Net6, the corresponding background services must also be synchronized to .Net6.
Background service is a corner of the development of many systems. It was only Window Service in Net Framework. However, starting from dotnet Core and now in .Net6, it is no longer Window Service, but has been changed to Worker Service.


Worker Service Description Figure 1



After the project is created, the default structure and procedures are as follows:
Worker Service Description Figure 2 Worker Service Description Figure 3



First add Nuget and refer to "Microsoft.Extensions.Hosting.WindowsServices". After joining, you can add .UseWindowsService() in Program.cs.
The template descriptions on the Internet all specify the ServiceName here. However, since the subsequent installation is to install the service through the sc command, and the sc command needs to bahamas whatsapp phone number specify the Service Name, it is no longer important whether the ServiceName is specified in the program. Actual testing has proven: Whether the program has If the ServiceName is specified, or if it is different from the service name in the sc command, the sc command will be the main one.

※ When using the Worker Service of .net6, it is Visual Studio 2022 version 17.3.3, .Net 6.0.9, and Nuget version Microsoft.Extensions.Hosting.WindowsServices 6.0.1. If there are differences in version updates in the future, please go to Check Microsoft official website.

Worker Service Description Figure 4

Worker.cs is the content of service execution. By default, only ExecuteAsync overrides the implementation. StartAsync service startup and StopAsync service stop are similar to the OnStart and OnStop methods of WindowsService in the past. They only need to be overridden when necessary in .Net6; It is also worth noting that if you overwrite StartAsync and StopAsync, you must call "await base.StartAsync (stoppingToken);" or "await base.StopAsync(stoppingToken);".

For first time use, observe the execution of each method by simply writing files:
Post Reply