Repository for my CoordinatedBackgroundService
package and the related article:
2020-Jan-05: Lifecycle of Generic Host Background Services
A replacement for the .NET IHostedService
which provides a separate initialization phase, distinct from the service execution phase. This can prevent startup race conditions in dependent services. Detailed examples are available in the article.
Create and host a class which derives from the CoordinatedBackgroundService
base class, which is in the Microsoft.Extensions.Hosting namespace. At a minimum, you probably want to override InitializingAsync
and ExecuteAsync
, and maybe StoppingAsync
.
If your service needs to initiate application shutdown, inject IHostApplicationLifetime
into the constructor and call StopApplication
where necessary.
Since these services execute on unmonitored threads, it is absolutely necessary to handle all exceptions. Any unhandled exceptions will immediately terminate the application.