Invex.Extensions.Hosting
0.5.0-rc.5
See the version list below for details.
dotnet add package Invex.Extensions.Hosting --version 0.5.0-rc.5
NuGet\Install-Package Invex.Extensions.Hosting -Version 0.5.0-rc.5
<PackageReference Include="Invex.Extensions.Hosting" Version="0.5.0-rc.5" />
<PackageVersion Include="Invex.Extensions.Hosting" Version="0.5.0-rc.5" />
<PackageReference Include="Invex.Extensions.Hosting" />
paket add Invex.Extensions.Hosting --version 0.5.0-rc.5
#r "nuget: Invex.Extensions.Hosting, 0.5.0-rc.5"
#:package Invex.Extensions.Hosting@0.5.0-rc.5
#addin nuget:?package=Invex.Extensions.Hosting&version=0.5.0-rc.5&prerelease
#tool nuget:?package=Invex.Extensions.Hosting&version=0.5.0-rc.5&prerelease
Invex.Extensions.Hosting
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.
AddHostedServiceoverloads 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, .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;
}
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 | Versions 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. |
-
.NETStandard 2.0
- JetBrains.Annotations (>= 2026.2.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
-
net10.0
- JetBrains.Annotations (>= 2026.2.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
-
net8.0
- JetBrains.Annotations (>= 2026.2.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
-
net9.0
- JetBrains.Annotations (>= 2026.2.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.12)
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 |