Invex.Extensions.Hosting 1.1.0-rc.4

This is a prerelease version of Invex.Extensions.Hosting.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Invex.Extensions.Hosting --version 1.1.0-rc.4
                    
NuGet\Install-Package Invex.Extensions.Hosting -Version 1.1.0-rc.4
                    
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="1.1.0-rc.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Invex.Extensions.Hosting" Version="1.1.0-rc.4" />
                    
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 1.1.0-rc.4
                    
#r "nuget: Invex.Extensions.Hosting, 1.1.0-rc.4"
                    
#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@1.1.0-rc.4
                    
#: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=1.1.0-rc.4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Invex.Extensions.Hosting&version=1.1.0-rc.4&prerelease
                    
Install as a Cake Tool

Invex.Extensions.Hosting

Build NuGet License: MIT .NET

Small, dependency-light utilities for Microsoft.Extensions.Hosting that remove the boilerplate around shared-instance registrations, exit codes on shutdown and fixed-rate background work.

Features

  • Multi-interface registration. Register one implementation under several service types without creating duplicate instances.
  • Hosted services you can talk to. AddHostedService overloads make the running hosted service resolvable through its own interfaces.
  • Host control with exit codes. Stop the generic host gracefully while setting the process exit code.
  • Fixed-cadence background services. Run background work on a monotonic schedule that never drifts and never overlaps.

Installation

dotnet add package Invex.Extensions.Hosting

The package targets .NET 10 and .NET Standard 2.0. The .NET Standard build can be consumed by compatible runtimes, including .NET Framework 4.8 and older .NET versions. 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;
}

public sealed class FatalException : Exception;

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

Usage

Register one implementation under several service types

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

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

The implementation type is registered once and each listed service type forwards to it, so resolving any of them returns the same object (per scope for scoped registrations). The hosted-service overloads also register that instance as IHostedService. See Multi-Interface Registration.

Stop the host with an exit code

Register IHostControl once with AddHostControl(), then inject it wherever you would use IHostApplicationLifetime:

builder.Services.AddHostControl();

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

StopApplication(exitCode) assigns Environment.ExitCode, requests graceful shutdown and returns immediately. See Host Control.

Run work on a fixed cadence

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);
}

Cycles are scheduled from the previous scheduled start, never overlap, and catch up back-to-back after an overrun. See Cycle Background Service.

Documentation

Full documentation: https://invex-games.github.io/hosting-extensions/

Guide Contents
Introduction What the library is for, key concepts and design principles
Getting Started Installation, supported frameworks 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, shutdown and errors
API Reference Complete public API

Development

The repository requires the .NET 10 SDK selected by global.json. From the repository root:

dotnet build Invex.Extensions.Hosting.slnx
dotnet test Invex.Extensions.Hosting.slnx

The documentation site is built with DocFX through the Atom build in _atom/:

atom BuildDocs   # build the site into _site/
atom ServeDocs   # build and serve it locally

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 was computed.  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 was computed.  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
1.2.0 425 11/11/2026
1.2.0-rc.11 62 11/11/2026
1.2.0-rc.2 71 10/30/2026
1.1.0 3,300 10/22/2026
1.1.0-rc.6 67 10/22/2026
1.1.0-rc.4 73 10/22/2026
1.1.0-rc.2 65 10/16/2026
1.0.0 95 10/15/2026
0.5.0-rc.26 64 10/15/2026
0.5.0-rc.24 71 10/14/2026
0.5.0-rc.21 54 10/14/2026
0.5.0-rc.19 58 10/14/2026
0.5.0-rc.17 51 10/14/2026
0.5.0-rc.15 50 10/14/2026
0.5.0-rc.13 51 10/14/2026
0.5.0-rc.11 91 10/14/2026
0.5.0-rc.5 70 10/13/2026
0.4.0 488 10/1/2026
0.4.0-rc.12 67 10/1/2026
0.4.0-rc.10 71 9/11/2026
Loading failed