liveBook · Manning

Windows service

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

A Windows Service is an operating-system-managed application process that can run in the background without an interactive user session.

A Windows service runs a long-lived .NET worker in the background without handling HTTP requests or presenting a user interface. Windows monitors and starts the installed application automatically, making it suitable for queue processing, scheduled work, and other headless tasks. section 34.2.2 section 34.2

The application itself is usually a worker service built on the generic IHost. It retains configuration, logging, dependency injection, and hosted-service support while avoiding the additional HTTP surface of ASP.NET Core. section 34.2 section 34.4

A practical Windows-service setup follows these steps:

  • Create a worker-service application and register its IHostedService implementation with the generic host. section 34.2.1
  • Add Microsoft.Extensions.Hosting.WindowsServices and call UseWindowsService() on IHostBuilder. section 34.2.2
  • Publish or build the executable, then install it with the Windows sc utility and provide its executable path. section 34.2.2
  • Manage startup, stopping, service identity, and error handling from the Windows Services control panel. section 34.2.2

Enabling Windows Service support

IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { services.AddHostedService<Worker>(); }) .UseWindowsService() .Build(); host.Run(); During development, UseWindowsService() has no effect when the application runs as a console app, so the same worker remains convenient to test locally. 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.

Use this annotated listing to see where Windows Service integration fits into ordinary worker-host configuration. section 34.2.2

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

How deep it goes
Explains worker architecture, package configuration, installation, and administration, but defers advanced scheduling to Quartz.NET.
Examples
Uses a Worker template, UseWindowsService(), the sc utility, and the Windows Services control panel as concrete setup artifacts.
What it assumes
Assumes basic .NET hosting familiarity while introducing worker services, generic hosting, dependency injection, logging, and configuration.
What you can do after
Operate/configure a .NET worker as a locally testable Windows service using a host extension, NuGet package, sc, and Services control panel.
How it connects
Connects Windows services with worker services, the generic host, IHostedService, dependency injection, logging, configuration, and systemd daemons.

Also explained in

Compare how 2 books explain Windows service →

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.