DistributedLock.MongoDatabase 3.1.1

dotnet add package DistributedLock.MongoDatabase --version 3.1.1                
NuGet\Install-Package DistributedLock.MongoDatabase -Version 3.1.1                
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="DistributedLock.MongoDatabase" Version="3.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DistributedLock.MongoDatabase --version 3.1.1                
#r "nuget: DistributedLock.MongoDatabase, 3.1.1"                
#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 DistributedLock.MongoDatabase as a Cake Addin
#addin nuget:?package=DistributedLock.MongoDatabase&version=3.1.1

// Install DistributedLock.MongoDatabase as a Cake Tool
#tool nuget:?package=DistributedLock.MongoDatabase&version=3.1.1                

DistributedLock.MongoDatabase

Nuget (with prereleases) Nuget GitHub license

  • DistributedLock.MongoDatabase is a library that allows you not to worry about multiple instances and easily block sections of code by identifier.
  • Based on dotnet standard 2.1, dotnet standard 2.0
  • The library itself will create the right table for locks and mongoDB itself will delete records at the right time. You don't need to do anything else yourself.

I wrote this library for myself, but I'm sharing it with the community because it can help other developers. Now I use serverless for my projects and had some problems with several instances, and lambdas that can fall asleep at any time. That's what led me to make the DistributedLock.MongoDatabase library.

I searched for different solutions, but didn't find anything good for mongoDB. I tried serverless redis, but it wasn't quite what I needed. I needed a good solution for mongoDB and I made one.

I'm always open to your changes, commits and wishes 😃

How do I get started?

You only need to add AddDistributedLock to your application

builder.Services.AddDistributedLock(
    "your mongodb connection string", 
    "your mongodb database name"
);

After that you can inject it anywhere in the project, here's an example using the DI

private readonly IDistributedLock _distributedLock;

public LockController(IDistributedLock distributedLock)
{
    _distributedLock = distributedLock;
}

You can use different variations of your lock. For example, like this:

var acquireLock = await _distributedLock.AcquireLockAsync("PassHereUniqueIdentifier");

if (!acquireLock)
    throw new Exception("Locked");

try
{
    await _testService.Post(dto);
}
finally
{
    await _distributedLock.ReleaseLockAsync("PassHereUniqueIdentifier");
}

Or with using

await using (var handler = await _distributedLock.AcquireUsingLockAsync(id))
{
    if(handler == null)
    throw new Exception("Locked");

    await _testService.Put(id, dto);
}

Or even using fluent

await _distributedLock.DoLock(id)
    .OnLock(async () => await _testService.Put(id, dto))
    .OnWait(() => throw new Exception("Locked"))
    .ExecuteAsync();

You can return any type

return await _distributedLock.DoLock(id)
    .OnLock(async () => await _testService.PutWithMessage(id, dto))
    .OnWait(async x=> await SomeStringInfo(x))
    .ExecuteAsync<string>();

You can use AcquireTupleLockAsync to get tuple with waitSeconds, to understand if somehow the instance dies, or we somehow don't get a ReleaseLockAsync, then the result will be false and the time in seconds (how long we should wait). By default it's 30 seconds.

var (acquireLock, waitSeconds) = await _distributedLock.AcquireTupleLockAsync("PassHereUniqueIdentifier");

if (!acquireLock)
    throw new Exception($"Locked. You need to wait: {waitSeconds} sec.");

try
{
    await _testService.Post(dto);
}
finally
{
    await _distributedLock.ReleaseLockAsync("PassHereUniqueIdentifier");
}

You can find all these examples and work with them in the Sample.WebApi

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 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 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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  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

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
3.1.1 3,864 6/27/2023