liveBook · Manning

Microsoft.Extensions.Hosting.WindowsServices

As explained in ASP.NET Core in Action, 3e

Microsoft.Extensions.Hosting.WindowsServices is a .NET hosting package that integrates applications with the Windows Service system.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains package installation, host configuration, publishing, service installation, and management without detailing internal integration hooks.
Examples
Uses a Worker registration, UseWindowsService(), a package reference, an sc create command, and the Services control panel.
What it assumes
Assumes a worker application and basic .NET publishing, while showing the required package, host call, and installation commands.
What you can do after
Operate a published worker as a Windows Service using UseWindowsService(), sc, and the Services control panel.
How it connects
Connects worker services with IHostBuilder, hosted services, Windows Services, systemd, and the sc utility.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.