Skip to content

An implementation of IHostedService which separates initialization from execution

License

Notifications You must be signed in to change notification settings

MV10/CoordinatedBackgroundService

Repository files navigation

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.

About

An implementation of IHostedService which separates initialization from execution

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages