Microsoft.Extensions.Hosting.WindowsServices integrates a .NET worker application with the Windows Service system. Add the package and call UseWindowsService(); console and development runs remain unchanged. section 34.2.2
Deploy a worker as a Windows Service with these steps:
- Add the Microsoft.Extensions.Hosting.WindowsServices NuGet package. section 34.2.2
- Call UseWindowsService() on the IHostBuilder. section 34.2.2
- Publish the application, then install its executable with the Windows sc utility from an administrator command prompt. section 34.2.2
- Manage startup, stopping, service identity, and error handling through the Windows Services control panel. section 34.2.2
Listing 34.9 Adding Windows Service support to a worker service
IHost host = Host.CreateDefaultBuilder(args) #1 .ConfigureServices((hostContext, services) => #1 { #1 Services.AddHostedService<Worker>(); #1 }) #1 .UseWindowsService() #2 .Build(); host.Run(); #1 Configures your worker service as you would normally #2 Adds support for running as a Windows Service.
See the minimal host configuration that registers a Worker service and enables Windows Service support. section 34.2.2
