Distributed.Locks
1.4.0
dotnet add package Distributed.Locks --version 1.4.0
NuGet\Install-Package Distributed.Locks -Version 1.4.0
<PackageReference Include="Distributed.Locks" Version="1.4.0" />
paket add Distributed.Locks --version 1.4.0
#r "nuget: Distributed.Locks, 1.4.0"
// Install Distributed.Locks as a Cake Addin
#addin nuget:?package=Distributed.Locks&version=1.4.0
// Install Distributed.Locks as a Cake Tool
#tool nuget:?package=Distributed.Locks&version=1.4.0
Important
This is abstractions package. To use implementation install "Distributed.Locks.Sql" package!
Install-Package Distributed.Locks.Sql
Distributed application lock toolset
Synchronization toolset for distributed applications provides utilities for cross-server mutex implementation.
Contents
Key features
This toolset provides a convenient and reliable way to synchronize your applications located on different hosts. It combines C#'s lock and Mutex syntax usage advantages. The lock itself imlements Dispose pattern to release locked resources so you don't need to call methdos manualy, just put you'r critical lines of code inside using block and and there you have it.
Prerequisites
Library is using .NET Standard 2.0. It can be used with .NET Core 2.0 or .NET Framework 4.5.2 out of the box.
Usage
First of all, you need to create a unique-named mutex to protect your resourse(s) from parallel access. More details you can get in implementation details of the mutexes.
The usage of the mutex is pretty simple, all that you need is to create a using block
IDistributedMutex mutex = CreateSomeMutex();
using (mutex.WaitOne(10000))
{
// Use resource you want to protect from paralell access
}
At that sample, the mutex will obtain and lock your resource or wait 10 seconds, and after that will give control to your code back, if it was failed to obtain the lock. To handle that you can use ILockState result:
using (var @lock = mutex.WaitOne(10000))
{
Console.WriteLine(@lock.LockResult != LockResult.AcquisitionTimeout ?
$"Job {job} acquired lock" :
$"Job {job} get resource by timeout");
// Use resource you want to protect from paralell access
Console.WriteLine($"Job {job} releases resource");
}
Lock result can contain the next values: * Acquired * AcquiredAfterIncompatibleLocksRemoved * AcquisitionTimeout * AcquisitionCanceled * Deadlocked * Error
Mutex based on Microsoft SQL Server implementation
This implementation is using stored procedure sp_getapplock shipped with Microsoft SQL Server. More information about it you can find here
Prerequisities
- Microsoft SQL Server 2008 or newer
Creation of a mutex
You can create mutex using two arguments: connection factory method and mutex name.
IDistributedMutex mutex =
new SqlDistributedMutex(() =>
new SqlConnection("some connection string"), "SampleMutex");
Best practices
But the best way to create a mutex is to inherit from it and create objects without any inbound parameters:
public class SampleLockMutex : SqlDistributedMutex
{
public SampleLockMutex() :
base(ConnectionFactory.CreateConnection, nameof(SampleLockMutex))
{
}
}
Also you can use nameof(class) as mutex name. This will prevent you to make a typo in a mutex name.
Another one aproach is to use DI-container to resolve mutexes and inject the connection factory. You can create a marker interface ISampleMutex like that:
public interface ISampleMutex : IDistributedMutex
{
}
And implement it in your class which will be resolved by DI-container with registered connection factory.
public class MyInjectedMutex : SqlDistributedMutex, ISampleMutex
{
public MyInjectedMutex(Func<SqlConnection> createConnection)
: base(createConnection, nameof(ISampleMutex))
{
}
}
Product | Versions 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 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. |
.NET Core | netcoreapp2.0 is compatible. 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 | net452 is compatible. net46 was computed. 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. |
-
.NETCoreApp 2.0
- No dependencies.
-
.NETFramework 4.5.2
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Distributed.Locks:
Package | Downloads |
---|---|
Distributed.Locks.Sql
Mutex for syncronize distributed applications |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.4.0 | 1,176 | 12/16/2019 |