Invex.Extensions.Hosting 0.4.0-rc.10

This is a prerelease version of Invex.Extensions.Hosting.
dotnet add package Invex.Extensions.Hosting --version 0.4.0-rc.10
                    
NuGet\Install-Package Invex.Extensions.Hosting -Version 0.4.0-rc.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Invex.Extensions.Hosting" Version="0.4.0-rc.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Invex.Extensions.Hosting" Version="0.4.0-rc.10" />
                    
Directory.Packages.props
<PackageReference Include="Invex.Extensions.Hosting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Invex.Extensions.Hosting --version 0.4.0-rc.10
                    
#r "nuget: Invex.Extensions.Hosting, 0.4.0-rc.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Invex.Extensions.Hosting@0.4.0-rc.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Invex.Extensions.Hosting&version=0.4.0-rc.10&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Invex.Extensions.Hosting&version=0.4.0-rc.10&prerelease
                    
Install as a Cake Tool

Invex.Extensions.Hosting

License: MIT

Small, dependency-light utilities for Microsoft.Extensions.Hosting:

  • Register one implementation under multiple dependency-injection service types without creating duplicate instances.
  • Stop a generic host gracefully while setting the process exit code.
  • Run background work on a fixed, monotonic cadence without scheduling drift.

Installation

dotnet add package Invex.Extensions.Hosting

The package targets .NET 10, .NET 9, .NET 8, and .NET Standard 2.0. The .NET Standard build can be consumed by compatible runtimes, including .NET Framework 4.8. The only runtime dependency is Microsoft.Extensions.Hosting.Abstractions.

Quick start

using Invex.Extensions.Hosting;
using Invex.Extensions.Hosting.Control;
using Invex.Extensions.Hosting.Service;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddHostControl();
builder.Services.AddHostedService<IWorkerStatus, Worker>();

await builder.Build().RunAsync();

public interface IWorkerStatus
{
    bool IsBusy { get; }
}

public sealed class Worker(IHostControl hostControl) : CycleBackgroundService, IWorkerStatus
{
    public bool IsBusy { get; private set; }

    protected override int CycleCadenceMs => 5_000;

    protected override async Task ExecuteCycleAsync(CancellationToken stoppingToken)
    {
        IsBusy = true;

        try
        {
            await DoWorkAsync(stoppingToken);
        }
        catch (FatalException)
        {
            hostControl.StopApplication(exitCode: 1);
        }
        finally
        {
            IsBusy = false;
        }
    }

    private static Task DoWorkAsync(CancellationToken stoppingToken) => Task.CompletedTask;
}

Worker is registered once. It is both the running IHostedService and the instance returned when another component injects IWorkerStatus.

Multi-interface dependency injection

ServiceCollectionExtensions provides AddSingleton and AddScoped overloads for two to five service types:

services.AddSingleton<IFoo, IBar, MyService>();
services.AddScoped<IRequestState, IRequestMetrics, RequestState>();

The implementation type is registered once, and each listed service type forwards to that registration. Resolving any listed service type or the implementation type returns the same object. Singletons share one object for the application lifetime; scoped registrations share one object per scope. Registrations are appended like the standard AddSingleton and AddScoped methods; they are not conditional TryAdd registrations.

The AddHostedService overloads support one to five service types:

services.AddHostedService<IQueueMonitor, QueueWorker>();
services.AddHostedService<IStatus, IAdminOperations, Worker>();

They register the implementation as a singleton, expose it through each listed service type, and also register that same instance as IHostedService. This is useful when application components need to observe or control a running hosted service.

Host control and exit codes

Register IHostControl once with AddHostControl():

builder.Services.AddHostControl();

public sealed class FailureHandler(IHostControl hostControl)
{
    public void StopForFailure() => hostControl.StopApplication(exitCode: 2);
}

IHostControl inherits IHostApplicationLifetime, so its lifetime tokens and parameterless StopApplication() behave like the framework service. StopApplication(exitCode) first assigns Environment.ExitCode, then requests graceful shutdown, and returns immediately; it does not wait for hosted services to finish stopping. Exit code 0 means success by convention, while a non-zero value conventionally indicates failure. If it is called more than once, the last assigned exit code wins.

Fixed-cadence background services

Derive from CycleBackgroundService and implement two members:

public sealed class MetricsFlusher(IMetricsBuffer buffer) : CycleBackgroundService
{
    protected override int CycleCadenceMs => 10_000;

    protected override Task ExecuteCycleAsync(CancellationToken stoppingToken) =>
        buffer.FlushAsync(stoppingToken);
}

The cadence is anchored to monotonic Stopwatch timestamps, so cycle duration does not accumulate drift. Cycles never overlap. If work overruns its scheduled slot, overdue cycles run back-to-back until the schedule catches up. The cadence is read once at the start of each cycle, so derived classes may change it between cycles.

Cancellation during the inter-cycle delay exits promptly without starting another cycle. Cancellation during ExecuteCycleAsync must be honored by the derived implementation. An unhandled cycle exception stops the loop and is handled according to the host's HostOptions.BackgroundServiceExceptionBehavior; catch and handle failures inside the cycle if the service should continue.

Documentation

Guide Contents
Documentation home Overview and design principles
Getting started Installation and a guided example
Multi-interface registration Shared-instance DI registrations
Host control Graceful shutdown and exit-code behavior
Cycle background service Fixed-cadence scheduling and errors
API reference Complete public API

License

MIT © Declan Smith

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0-rc.10 49 9/11/2026
0.4.0-rc.8 61 8/28/2026
0.4.0-rc.6 57 8/21/2026
0.4.0-rc.4 68 8/14/2026
0.4.0-rc.2 72 8/7/2026
0.3.0 280 8/3/2026
0.3.0-rc.5 67 8/3/2026
0.3.0-rc.4 72 7/24/2026
0.3.0-rc.2 64 7/17/2026
0.2.0 134 7/13/2026
0.2.0-rc.29 61 7/13/2026
0.2.0-rc.26 88 7/10/2026
0.2.0-rc.24 73 7/9/2026
0.2.0-rc.18 72 7/5/2026
0.2.0-rc.16 69 7/1/2026
0.2.0-rc.14 73 6/30/2026
0.2.0-rc.12 76 6/25/2026
0.2.0-rc.10 71 6/24/2026
0.2.0-rc.8 79 6/23/2026
0.2.0-rc.6 83 6/19/2026
Loading failed