AzureMutex 1.2.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package AzureMutex --version 1.2.0
NuGet\Install-Package AzureMutex -Version 1.2.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="AzureMutex" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureMutex --version 1.2.0
#r "nuget: AzureMutex, 1.2.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.
// Install AzureMutex as a Cake Addin
#addin nuget:?package=AzureMutex&version=1.2.0

// Install AzureMutex as a Cake Tool
#tool nuget:?package=AzureMutex&version=1.2.0

Distributed mutex based on Azure Blobs

CI NuGet

.Net implementation of a distributed lock on top of Azure Blobs.

Diclaimer: if you think you need it, you might doing it wrong 😃

Jokes aside, we do use it for some cases. Typical example involves a process running on several nodes (for redundancy). E.g. periodic sync with a 3rd party system, a database migration, etc.

Recommended usage is via an extension method that takes a long-running task as a parameter and ensures it runs as a single instance (in a single process):

var mutex = new BlobMutex("storage-account-connection", "mutex-container", "blob-name");

// 1. Blocks until lock is held or `CancellationToken` cancelled
// 2. Executes `LongRunningTask` while periodically renewing the lock taken. 
// 3. Runs either until operation is finished or `CancellationToken` canceled
// 4. Terminates operation in case the lock is lost (e.g. because of connectivity issues)
//    with a `LeaseLostException`
await mutex.RunSingleInstance(LongRunningTask, CancellationToken.None); 

async Task LongRunningTask(CancellationToken cancellation) {
    do {
        await Task.Delay(TimeSpan.FromMinutes(10), cancellation);
    }
    while (!cancellation.IsCancellationRequested)
}

References

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 was computed.  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. 
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
1.2.1-prerelease 90 1/26/2024
1.2.0 1,402 1/25/2024
1.1.0 6,676 2/6/2022
1.0.0 423 1/3/2022