AddHostedService<T>() registers a hosted-service implementation with ASP.NET Core dependency injection. Use it for long-running background work that starts with the application and continues until shutdown. section 34.1.1 section 34.4
Registering a background service
Register its dependencies first, then add the hosted service: builder.Services.AddHttpClient<ExchangeRatesClient>(); builder.Services.AddSingleton<ExchangeRatesCache>(); builder.Services.AddHostedService<ExchangeRatesHostedService>(); A BackgroundService typically overrides ExecuteAsync, repeats work until its CancellationToken signals shutdown, and can use IServiceProvider.CreateScope() when it needs scoped services. AddHostedService registers the implementation with a singleton lifetime. section 34.4
