DLeader.Consul 1.0.6

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

DLeader.Consul

A .NET library that provides distributed leader election capabilities using HashiCorp Consul. This library helps you implement leader election patterns in distributed systems with a clean and simple API.

Features

  • 🔄 Automatic leader election using Consul
  • 🔌 Easy service registration and discovery
  • 🏃 Automatic session management and renewal
  • 🎯 Event-driven leadership changes
  • ⚡ High performance and low overhead
  • 🛠️ Built with dependency injection in mind
  • 📝 Extensive logging and diagnostics

Installation

dotnet add package DLeader.Consul

Quick Start

// Register the service
services.AddConsulLeader(options =>
{
    options.ServiceName = "my-service";
    options.Address = "http://consul:8500";
});

// Use in your service
public class MyService : BackgroundService
{
    private readonly ILeaderElection _leaderElection;
    
    public MyService(ILeaderElection leaderElection)
    {
        _leaderElection = leaderElection;
        _leaderElection.OnLeadershipAcquired += HandleLeadershipAcquired;
        _leaderElection.OnLeadershipLost += HandleLeadershipLost;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await _leaderElection.StartLeaderElectionAsync(stoppingToken);
    }
}

Configuration Options

public class ConsulOptions
{
    public string ServiceName { get; set; } = "leadership-service";
    public string Address { get; set; } = "http://localhost:8500";
    public int SessionTTL { get; set; } = 15;
    public int RenewInterval { get; set; } = 5;
    public int LeaderCheckInterval { get; set; } = 1;
}

Advanced Usage

Custom Service Registration

services.AddConsulLeader(options =>
{
    options.ServiceName = "my-service";
    options.Address = "http://consul:8500";
    options.SessionTTL = 15;
    options.RenewInterval = 5;
}, serviceOptions =>
{
    serviceOptions.ServicePort = 5000;
    serviceOptions.HealthCheckEndpoint = "/health";
    serviceOptions.Tags = new[] { "production", "web" };
});

Prerequisites

  • .NET 8.0 or higher
  • Consul server (local or remote)

Dependencies

  • Consul (>= 1.6.10.9)
  • Microsoft.Extensions.DependencyInjection (>= 8.0.0)
  • Microsoft.Extensions.Logging (>= 8.0.0)

Running the Examples

  1. Start Consul:
docker run -d -p 8500:8500 consul:latest
  1. Run multiple instances:
dotnet run --project samples/ConsulLeaderExample

--

You can run

docker compose up --build

Testing

dotnet test

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details

Support

If you need help or have any questions:

  • Open an issue
  • Submit a pull request
  • Contact the maintainers

Maintainers

  • Franco Pachue

Acknowledgments

  • HashiCorp Consul team
  • .NET community

Tags

consul, leader-election, distributed-systems, dotnet, csharp, microservices, service-discovery

Product Compatible and additional computed target framework versions.
.NET 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 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 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on DLeader.Consul:

Package Downloads
DLeader.Consul.Messaging

Best-effort message fan-out between service instances over the Consul KV store. Split out of DLeader.Consul in 2.0. Not a queue: delivery is best-effort, retention is minutes, and an instance that is down when a message is published never sees it.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 97 9/5/2026
2.0.0 109 9/4/2026
1.13.0 80 9/4/2026
1.12.0 77 9/4/2026
1.10.0 278 2/9/2025
1.0.9 230 11/5/2024
1.0.7 211 11/4/2024
1.0.6 207 11/4/2024