Escapement.Azure.Functions.DistributedLock 0.1.16

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

Escapement.Azure.Functions.DistributedLock

A small library to provide distributed locking for Azure Functions (Isolated Worker) using Azure Blob Storage leases. It is a lightweight replacement for [Microsoft.Azure.WebJobs.Singleton] that supports declarative attributes and programmatic lock acquisition.

Package: Escapement.Azure.Functions.DistributedLock

Summary

  • Declarative locking via [Singleton("key-{id}")] attribute for in-place migration from in-process to isolated Functions worker model.
  • Declarative locking via [DistributedLock("key-{id}")] attribute.
  • Programmatic locking via IDistributedLockHandlerFactory and IDistributedLockHandler.
  • Uses Azure Blob leases with automatic background renewal and safe release.
  • Testable design suitable for unit tests.

Install

dotnet add package Escapement.Azure.Functions.DistributedLock

Quick start

1) Register services in Program.cs:

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Azure.Storage.Blobs;
using Escapement.Azure.Functions.DistributedLock.Middleware; //exclude if not using the middleware
using Escapement.Azure.Functions.DistributedLock;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(workerApplication =>
    {
        // Register middleware if using the [Singleton] or [DistributedLock] attributes
        workerApplication.UseMiddleware<DistributedLockMiddleware>();
    })
    .ConfigureServices(services =>
    {
        // Assumes "AzureWebJobsStorage" env var is set
        services.AddSingleton(sp => new BlobServiceClient(
            Environment.GetEnvironmentVariable("AzureWebJobsStorage")
        ));

        // Register the default distributed lock handler (used by both Declarative usage and Programmatic usage scenarios)
        services.AddDistributedLock();
    })
    .Build();

await host.RunAsync();

2) Declarative usage (attribute on function):

Make sure to add the DistributedLockMiddleware middleware to Program.cs

[Function("ProcessOrder")]
[Singleton("order-lock-{orderId}")]
public async Task Run(string orderId, ILogger log)
{
    // If this runs, the lock was acquired and is being renewed.
}

or

[Function("ProcessOrder")]
[DistributedLock("order-lock-{orderId}")]
public async Task Run(string orderId, ILogger log)
{
    // If this runs, the lock was acquired and is being renewed.
}

3) Programmatic usage (fine-grained control):

Does not use the middleware.

var handler = _lockHandlerFactory.CreateHandler();
await using var handle = await handler.TryAcquireLockAsync("my-key");
if (handle == null) { /* locked: skip or retry */ }
// critical section

Attribute examples

Attribute usage Lock key Behavior
[Singleton] or [DistributedLock] Function name Only one instance runs per function accross all hosts (default).
[Singleton("GlobalProcess")] or [DistributedLock("GlobalProcess")] GlobalProcess Only one instance runs across all hosts.
[Singleton("User-{userId}")] or [DistributedLock("User-{userId}")] User-1234 Only one instance runs for a specific user ID.

Recommendations

  • Use a consistent storage account and container name across hosts.
  • Prefer WaitForAcquireLockAsync with timeout for background jobs that must complete.

License and contribution

See repository for full docs, tests, and contribution guidelines.

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

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.1.17 161 1/3/2026
0.1.16 133 1/3/2026
0.1.15 274 12/15/2025
0.1.13 237 12/14/2025
0.1.12 246 12/14/2025