projects/ihosted-service/ihosted-service-1/README.md
This sample demonstrates using the BackgroundService base class for background tasks in ASP.NET Core.
dotnet watch run
Navigate to http://localhost:5000/ and see the counter incrementing every second in the background). A GreeterUpdaterService updates a Greeter singleton every second.
the updates are visible on.
Greeter class provides the greeting data to the background task.
The sample uses dependency injection to the GreeterUpdaterService injects the Greeter singleton. The service also automatically updates the counter property.
BackgroundService base class for background tasksIHostedService implementationdotnet watch run
Navigate to http://localhost:5000/ and see the counter incrementing every second in the background) a Greeter service updates a Greeter singleton.
builder.Services.AddSingleton<Greeter>();
builder.Services.AddHostedService<GreeterUpdaterService>();
/scalar/openapi/v1.jsonThis sample was simplified from a custom IHostedService implementation to using the built-in BackgroundService base class.
HostedService abstract base class (40+ lines of boilerplate)Microsoft.Extensions.Hosting.BackgroundServiceSee OUT-OF-DATE.md for migration details.