HostLoom.Leadership.DependencyInjection 0.13.0

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

HostLoom.Leadership.DependencyInjection

This package registers HostLoom.Leadership electors with Microsoft.Extensions.DependencyInjection and starts them with the host:

services
    .AddHostLoomLocking(locking => locking.Namespace = "billing")
    .UseRedis();

services
    .AddHostLoomLeadership()
    .AddRole("scheduler")
    .AddRole("reconciler", leadership => leadership.Lease = TimeSpan.FromSeconds(30));

AddRole registers one LeaderElector per role over the IDistributedLock registered by AddHostLoomLocking, resolved as ILeadership and LeaderElector keyed by the role, with LeadershipOptions named by the role and validated when the host starts, including Leadership:Lease against the lock's Locking:MaxLease. When exactly one role is registered the unkeyed ILeadership resolves it too; with several, inject [FromKeyedServices("scheduler")] ILeadership leadership. A repeated role is refused at registration, and so is an ILeadership registered before AddHostLoomLeadership or one keyed by a role before AddRole, because it would have taken precedence over the elector; register a test double after the builder to replace the electors deliberately, or register it keyed by a role you do not add. The hosted service starts every elector with the host and stops each with the host, releasing the leases they hold so a graceful restart hands over at once.

A leader-only background loop is then:

public sealed class ReconcilerLoop(ILeadership leadership) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            await leadership.WaitForLeadershipAsync(stoppingToken);
            var leadershipToken = leadership.LeadershipToken;
            using var linked = CancellationTokenSource.CreateLinkedTokenSource(
                stoppingToken, leadershipToken);
            try
            {
                await RunWhileLeaderAsync(linked.Token);
            }
            catch (OperationCanceledException) when (leadershipToken.IsCancellationRequested)
            {
                // Lost this term; wait to lead again.
            }
        }
    }
}

See the HostLoom.Leadership package README for the guarantee statement and the elector's behaviour on loss, resignation, and stop.

Product Compatible and additional computed target framework versions.
.NET 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. 
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.17.0 70 10/25/2026
0.16.1 105 10/22/2026
0.16.0 174 10/14/2026
0.15.1 118 10/12/2026
0.15.0 115 10/4/2026
0.14.0 100 10/4/2026
0.13.0 102 9/28/2026
0.12.0 103 9/27/2026
0.11.0 106 9/25/2026
0.10.0 106 9/22/2026
0.9.0 104 9/22/2026