Escapement.Azure.Functions.DistributedLock
0.1.16
There is a newer version of this package available.
See the version list below for details.
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" />
<PackageReference Include="Escapement.Azure.Functions.DistributedLock" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Escapement.Azure.Functions.DistributedLock&version=0.1.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
IDistributedLockHandlerFactoryandIDistributedLockHandler. - 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
WaitForAcquireLockAsyncwith timeout for background jobs that must complete.
License and contribution
See repository for full docs, tests, and contribution guidelines.
| Product | Versions 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.
-
net8.0
- Azure.Storage.Blobs (>= 12.23.0)
- Microsoft.Azure.Functions.Worker.Core (>= 2.51.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.