Skip to content

Latest commit

 

History

History
17 lines (9 loc) · 1.23 KB

README.md

File metadata and controls

17 lines (9 loc) · 1.23 KB

CoordinatedBackgroundService NuGet

Repository for my CoordinatedBackgroundService package and the related article:

Purpose

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.

Usage

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.