liveBook · Manning

UseWindowsService()

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

UseWindowsService() is a .NET host-builder method that configures an application to run with Windows Service integration.

UseWindowsService() adds Windows Service integration to a .NET worker service through IHostBuilder. During development or console execution, it does nothing, so the application behaves normally while gaining the hooks required for service installation. section 34.2.2

Enable and install the integration with these practical steps:

  • Add the Microsoft.Extensions.Hosting.WindowsServices NuGet package. section 34.2.2
  • Call UseWindowsService() on the IHostBuilder while configuring the worker normally. section 34.2.2
  • Publish the application, then run the Windows sc utility from an Administrator command prompt with the published executable path. section 34.2.2
  • Manage startup, shutdown, 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.

Shows the minimal host configuration, including the normal hosted worker registration and the UseWindowsService() call. section 34.2.2

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

How deep it goes
Explains package setup, host configuration, console behavior, publishing, and installation, without detailing internal integration hooks.
Examples
Uses a hosted Worker registration, a UseWindowsService() call, a Windows Services package reference, and an sc create command.
What it assumes
Assumes familiarity with worker services, IHostBuilder, hosted service registration, package installation, publishing, and administrative command prompts.
What you can do after
Operate/configure a worker service for Windows using the Windows Services package, publishing process, and sc utility.
How it connects
Connects worker services, IHostBuilder, hosted services, generic hosting, Windows sc, and systemd integration.

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.