liveBook · Manning

Background processing

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

A background task is application work that executes independently of direct user interaction, often continuously, periodically, or in response to queued items.

Background processing runs application work independently of direct user interaction. It handles queued items, periodic jobs, or long-running processes while keeping user-facing requests responsive. section 34.0 section 34.1

Choose the hosting approach that matches the work:

  • Use IHostedService for tasks that start with the application, run during its lifetime, and stop with it. section 34.1
  • Use BackgroundService for long-running work, including loops that periodically process items or refresh data. section 34.1.1
  • Use a generic IHost worker when no HTTP endpoints or user interface are required. section 34.2
  • Manage dependency-injection scopes explicitly when a singleton background task needs scoped services. section 34.1

Refreshing an exchange-rate cache

A BackgroundService periodically calls a remote exchange-rate API and stores the result in a shared cache. An API endpoint reads the cache instead of making the remote request, keeping responses fast while the data is refreshed in advance. Avoid awaiting long-running work directly in StartAsync unless startup should remain blocked until that work completes. section 34.1.1

Figure 34.1 You can use a background task to cache the results from a third-party API on a schedule. The API controller can then read directly from the cache instead of calling the third-party API itself. This reduces the latency of requests to your API controller while ensuring that the data remains fresh.
CH34_F01_Lock3

Shows the relationship between scheduled cache refreshes and faster API responses. section 34.1.1

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

How deep it goes
Explains hosting lifecycles, timer-based execution, startup blocking, and dependency-injection scopes, then extends to headless workers.
Examples
Uses email queues, retail calculations, message handling, and scheduled exchange-rate caching as practical examples.
What it assumes
Assumes an application using dependency injection and teaches background execution, lifecycle hooks, and scope management.
What you can do after
Build background tasks with IHostedService or BackgroundService and choose a generic IHost worker when HTTP handling is unnecessary.
How it connects
Connects IHostedService and BackgroundService with dependency injection, caching, message queues, generic hosting, and batch processing.

Also explained in

Compare how 4 books explain background processing →

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.