⬆️ ⬇️

.NET Core Workers as Windows Services

In .NET Core 3.0, we present a new type of application template called Worker Service. This template is designed to give you a starting point for writing long-running services in .NET Core. In this walkthrough, we will create a worker and run it as a Windows service.









Creating worker



Note. In our preliminary versions, the worker template is in the same menu as the web templates. This will change in a future release. We intend to place the Worker Service template directly in the wizard to create a new project.



Creating a Worker in Visual Studio



image



image



image



Creating a Worker on the command line



Run the dotnet new worker



image



Run as a Windows service



To run as a Windows service, we need our worker to see the start and stop signals from the ServiceBase type .NET, which links the Windows Service systems to .NET applications. For this you need:



Add the NuGet Microsoft.Extensions.Hosting.WindowsServices package



image



Add a call to UseServiceBaseLifetime to HostBuilder in Program.cs



 public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceBaseLifetime() .ConfigureServices(services => { services.AddHostedService<Worker>(); }); } 


This method does several things. First, it checks if the application really works as a Windows service, if it is not, then it performs noops, which makes this method safe to call when running locally or as a Windows service.



Second, it configures your host to use ServiceBaseLifetime . ServiceBaseLifetime works with ServiceBase to help control the lifetime of your application when running as a Windows service. This overrides the standard ConsoleLifetime which handles signals such as CTL + C.



Install Worker



Once we get a worker using ServiceBaseLifetime we need to install it:



First, let's publish the application. We will install the Windows Service in-place, which means that the executable file will be blocked every time the service starts. Publishing is a good way to make sure that all the files needed to start the service are in one place and ready to be installed.



 dotnet publish -oc:\code\workerpub 


Then you can use the sc utility in the admin command line.



 sc create workertest binPath=c:\code\workerpub\WorkerTest.exe 


For example:



image



Security Note: This command starts the service as a local system, which is not what is needed . Instead, you must create a service account and start the Windows service as this account. We will not talk about this here, but here is some documentation on ASP.NET: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore- 2.2



Logging



The logging system has an “Event Log” service, which can send log messages directly to the Windows event log. To enter the event log, you can add the Microsoft.Extensions.Logging.EventLog package and then modify your Program.cs :



 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(loggerFactory => loggerFactory.AddEventLog()) .ConfigureServices(services => { services.AddHostedService<Worker>(); }); 


Further work



In the following preliminary versions, we plan to improve the use of Workers with Windows Services:



  1. Rename UseWindowsServiceBaseLifetime to UseWindowsService
  2. Add automatic and improved integration with the event log when running as a Windows service.


Conclusion



We hope that you will try out our new template and want to know how it works. You can send any bug reports or suggestions here: https://github.com/aspnet/AspNetCore/issues/new/choose



Avatar

Glenn condron


')

Source: https://habr.com/ru/post/446512/



All Articles