liveBook · Manning

Chapter 34: Building background tasks and ser vices

What you will learn in chapter 34 of ASP.NET Core in Action, 3e

This chapter introduces ASP.NET Core approaches for running noninteractive background work, from built-in hosted services to headless workers and Quartz.NET scheduling.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces ASP.NET Core approaches for running noninteractive background work, from built-in hosted services to headless workers and Quartz.NET scheduling.

Key terms: background service, batch-task service, queue, long-running process, IHostedService, BackgroundService, dependency injection, DI lifetimes, worker service, generic IHost

34.1 Running background tasks with IHostedService

This section introduces IHostedService as ASP.NET Core’s mechanism for running application-lifetime background work without blocking user requests.

Key terms: background task, queue, message bus, batch process, IHostedService, Kestrel, application lifetime, scoped lifetime, singleton background task, container scope

34.1.1 Running background tasks on a timer

This section shows how to use hosted background services to refresh cached data on a timer, while managing application startup, cancellation, DI lifetimes, and request-time performance.

Key terms: background task, ExchangeRateCache, typed client, API endpoint, cache, IHostedService, BackgroundService, ExecuteAsync, CancellationToken, StartAsync

34.1.2 Using scoped services in background tasks

This section explains how singleton hosted services can safely use scoped dependencies by creating and disposing a DI scope for each unit of background work.

Key terms: IHostedService, singleton, scoped lifetime, captive dependency, DbContext, constructor injection, IServiceProvider, CreateScope(), IServiceScope, scoped container

34.2 Creating headless worker services using IHost

This section introduces headless worker services as generic-host .NET applications for background work that does not need HTTP or a user interface.

Key terms: worker, worker service, headless service, IHost, IHostedService, generic IHost, ASP.NET Core, middleware pipeline, configuration, logging

34.2.1 Creating a worker service from a template

This section shows how to create a headless worker service from a template and how its hosting, service registration, project references, and execution differ from a typical ASP.NET Core web app.

Key terms: Worker Service, dotnet new worker, Worker.cs, Program.cs, BackgroundService, IHost, IHostBuilder, Host.CreateDefaultBuilder, ConfigureServices, HostBuilderContext

34.2.2 Running worker services in production

This section explains how to prepare and install long-running worker services as operating-system-managed services on Windows or Linux.

Key terms: worker service, Windows Service, systemd daemon, Microsoft.Extensions.Hosting.WindowsServices, Microsoft.Extensions.Hosting.Systemd, UseWindowsService(), dotnet publish, sc utility, Services control panel, IHostBuilder

34.3 Coordinating background tasks using Quartz.NET

This section introduces Quartz.NET as a configurable, DI-integrated scheduler for running and coordinating long-lived background jobs in ASP.NET Core applications.

Key terms: Quartz.NET, BackgroundService, schedule, trigger, Jobs, Triggers, Job factory, Scheduler, IHostedService, DI container

34.3.1 Installing Quartz.NET in an ASP.NET Core application

This section installs and configures Quartz.NET as a DI-integrated hosted scheduler in a .NET application, ready for jobs to be added later.

Key terms: Quartz.NET, IHostedService, IJob, job, Quartz.Extensions.Hosting, AddQuartzHostedService, WaitForJobsToComplete, IServiceCollection, WebApplicationBuilder.Services, job factory

34.3.2 Configuring a job to run on a schedule with Quartz.NET

This section shows how to define a Quartz.NET job, schedule it with triggers, and manage its dependency-injection and concurrency behavior.

Key terms: IJob, UpdateExchangeRatesJob, ExchangeRatesClient, AppDbContext, scoped dependencies, DI container, constructor injection, IHostedService, triggers, AddJob<T>()

34.3.3 Using clustering to add redundancy to your background tasks

This section explains how Quartz.NET persistence and clustering let multiple application instances run scheduled background jobs without executing the same trigger more than once.

Key terms: IHostedService, BackgroundService, worker service, horizontal scaling, Quartz.NET clustering, trigger, IJob, scheduler, database lock, backing store

34.4 Summary

This section summarizes how to run background work with hosted services, create headless worker services, and schedule scalable jobs with Quartz.NET.

Concepts explained in this chapter

← Chapter 33: Calling remote APIs with IHttpClientFactory Chapter 35: Testing applications with xUnit →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.