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.
Shows the relationship between scheduled cache refreshes and faster API responses. section 34.1.1



