DistributedLeasing.Azure.Cosmos 4.0.0

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

DistributedLeasing

A flexible, production-ready distributed leasing library for .NET that enables distributed coordination, leader election, and exclusive resource access across multiple instances.

Features

  • Multiple Azure Providers: Support for Azure Blob Storage, Azure Cosmos DB, and Azure Redis
  • Automatic Lease Renewal: Built-in mechanisms to maintain leases automatically
  • Leader Election: Simple, reliable leader election for distributed systems
  • Managed Identity Support: First-class support for Azure Managed Identity authentication
  • Dependency Injection: ASP.NET Core integration with familiar service registration patterns
  • Multi-Framework Support: Compatible with .NET Framework 4.6.1+, .NET Core 2.0+, .NET 8.0, and .NET 10.0

Quick Start

Installation

Install the provider package you need. Dependencies are installed automatically:

# For Azure Blob Storage
dotnet add package DistributedLeasing.Azure.Blob

# For Azure Cosmos DB
dotnet add package DistributedLeasing.Azure.Cosmos

# For Azure Redis
dotnet add package DistributedLeasing.Azure.Redis

# For ASP.NET Core with Dependency Injection (includes all providers)
dotnet add package DistributedLeasing.Extensions.DependencyInjection

Basic Usage - Azure Blob Storage

using DistributedLeasing.Azure.Blob;
using Azure.Identity;

// Create a blob lease provider
var provider = new BlobLeaseProvider(new BlobLeaseProviderOptions
{
    ContainerUri = new Uri("https://youraccount.blob.core.windows.net/leases"),
    Credential = new DefaultAzureCredential()
});

// Create a lease manager for a specific resource
var leaseManager = await provider.CreateLeaseManagerAsync("my-resource");

// Acquire a lease
var lease = await leaseManager.AcquireAsync(TimeSpan.FromSeconds(60));

if (lease != null)
{
    try
    {
        // Do work while holding the lease
        Console.WriteLine("Lease acquired! Doing critical work...");
        
        // Lease auto-renews in the background
        await Task.Delay(TimeSpan.FromMinutes(5));
    }
    finally
    {
        // Always release the lease when done
        await lease.ReleaseAsync();
    }
}

ASP.NET Core with Dependency Injection

using DistributedLeasing.Extensions.DependencyInjection;

// In Program.cs or Startup.cs
builder.Services.AddDistributedLeasing(options =>
{
    options.UseBlobStorage(blobOptions =>
    {
        blobOptions.ContainerUri = new Uri("https://youraccount.blob.core.windows.net/leases");
        blobOptions.Credential = new DefaultAzureCredential();
    });
});

// In your service
public class MyService
{
    private readonly ILeaseProvider _leaseProvider;
    
    public MyService(ILeaseProvider leaseProvider)
    {
        _leaseProvider = leaseProvider;
    }
    
    public async Task DoWorkAsync()
    {
        var manager = await _leaseProvider.CreateLeaseManagerAsync("my-resource");
        var lease = await manager.AcquireAsync(TimeSpan.FromSeconds(60));
        
        if (lease != null)
        {
            try
            {
                // Your critical work here
            }
            finally
            {
                await lease.ReleaseAsync();
            }
        }
    }
}

Package Structure

This library follows a granular package structure with automatic dependency resolution:

  • DistributedLeasing.Core: Core interfaces and contracts
  • DistributedLeasing.Abstractions: Base provider abstractions (for building custom providers)
  • DistributedLeasing.Azure.Blob: Azure Blob Storage implementation
  • DistributedLeasing.Azure.Cosmos: Azure Cosmos DB implementation
  • DistributedLeasing.Azure.Redis: Azure Redis implementation
  • DistributedLeasing.Extensions.DependencyInjection: ASP.NET Core integration

When you install a provider package (e.g., DistributedLeasing.Azure.Blob), all required dependencies are automatically installed.

Use Cases

Leader Election

Ensure only one instance performs a task:

var manager = await provider.CreateLeaseManagerAsync("leader");
var lease = await manager.AcquireAsync(TimeSpan.FromSeconds(60));

if (lease != null)
{
    // This instance is the leader
    await PerformLeaderTasksAsync();
}

Critical Section Protection

Protect shared resources from concurrent access:

var manager = await provider.CreateLeaseManagerAsync("database-migration");
var lease = await manager.AcquireAsync(TimeSpan.FromMinutes(10));

if (lease != null)
{
    try
    {
        await RunDatabaseMigrationAsync();
    }
    finally
    {
        await lease.ReleaseAsync();
    }
}

Scheduled Job Coordination

Prevent multiple instances from running the same scheduled job:

public async Task ExecuteScheduledJobAsync()
{
    var manager = await _provider.CreateLeaseManagerAsync("daily-report-job");
    var lease = await manager.AcquireAsync(TimeSpan.FromMinutes(30));
    
    if (lease != null)
    {
        try
        {
            await GenerateDailyReportAsync();
        }
        finally
        {
            await lease.ReleaseAsync();
        }
    }
}

Documentation

For comprehensive documentation, API reference, and advanced scenarios, visit:

License

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

Contributing

Contributions are welcome! Please see our contributing guidelines for more information.

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 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 (1)

Showing the top 1 NuGet packages that depend on DistributedLeasing.Azure.Cosmos:

Package Downloads
DistributedLeasing.Extensions.DependencyInjection

Dependency injection extensions for the DistributedLeasing library. Provides service registration helpers for ASP.NET Core and other DI-enabled applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.1.0 183 12/25/2025
5.0.0 180 12/25/2025
4.0.0 177 12/24/2025
1.0.1 216 12/23/2025
1.0.0 223 12/23/2025

Major version 2.0: Eliminated code duplication, merged authentication into abstractions, clean SOLID architecture. Breaking changes: Internal abstractions removed, providers now reference shared Abstractions package.