EasilyNET.MongoDistributedLock.AspNetCore 2.2024.428.71

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

// Install EasilyNET.MongoDistributedLock.AspNetCore as a Cake Tool
#tool nuget:?package=EasilyNET.MongoDistributedLock.AspNetCore&version=2.2024.428.71
EasilyNET.MongoDistributedLock.AspNetCore
  • EasilyNET.MongoDistributedLock.AspNetCore 让基于 EasilyNET.MongoDistributedLock 的使用更简单.

  • 注册分布式锁服务,用来配置一些基本信息

// 使用默认配置
builder.Services.AddMongoDistributedLock();

// 自定义一些配置
builder.Services.AddMongoDistributedLock(op =>
{
    op.DatabaseName = "test_locks";
    op.MaxDocument = 100;
    ...
});
  • 使用 IMongoLockFactory 来注入工厂服务
public class DistributedLockTest(IMongoLockFactory lockFactory)
{
    public async Task<dynamic> AcquireLock()
    {
        // 这里使用一个随机的ID作为锁ID,相当于其他锁中的Key.用来区分不同的业务的锁,也可以将不同的业务类型放到MongoDB中存起来,然后再使用的时候再取获取这个id
        const string lockId = "64d44afde4473b85a177084c";
        var mongoLock = lockFactory.GenerateNewLock(ObjectId.Parse(lockId));

        var acq = await mongoLock.AcquireAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(0));
        try
        {
            if (acq.Acquired)
            {
                // 关键部分,它不能一次由任何服务器上的多个线程执行
                // ...
                // ...
            }
            else
            {
                // 超时!也许另一个线程没有释放锁...我们可以再试一次或抛出例外
            }
        }
        finally
        {
            // 如果(acq.Acquired)无需手动操作
            await mongoLock.ReleaseAsync(acq);
        }
    }
}
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 is compatible. 
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
2.2024.428.71 68 4/28/2024
2.2024.427.1128 82 4/27/2024
2.2.72 88 4/14/2024
2.2.71 78 4/12/2024
2.2.8 69 4/26/2024
2.2.6 71 4/10/2024
2.2.5 92 3/26/2024
2.2.4 86 3/25/2024
2.2.3 82 3/24/2024
2.2.2 88 3/21/2024
2.2.1 93 3/20/2024
2.2.0 91 3/13/2024
2.1.9 87 2/21/2024
2.1.8 91 2/18/2024
2.1.7 84 2/16/2024
2.1.6 86 2/14/2024
2.1.5 87 2/14/2024
2.1.4 97 2/9/2024
2.1.3 103 2/8/2024
2.1.2 77 2/5/2024
2.1.1.2 155 12/26/2023
2.1.1.1 88 12/26/2023
2.1.1 108 12/25/2023
2.1.0 107 12/17/2023
2.0.11 121 12/6/2023
2.0.1 131 11/15/2023
2.0.0 106 11/14/2023
1.9.1 111 11/1/2023
1.9.0 105 10/19/2023
1.9.0-preview2 86 10/12/2023
1.9.0-preview1 87 10/12/2023
1.8.9 113 10/11/2023
1.8.8 107 10/11/2023
1.8.7-rc2 83 9/21/2023
1.8.7-rc1 86 9/12/2023
1.8.6 136 8/31/2023
1.8.5 118 8/25/2023
1.8.4 131 8/24/2023
1.8.3 134 8/23/2023
1.8.2 125 8/22/2023
1.8.1 137 8/18/2023
1.8.0 145 8/15/2023
1.7.9 161 8/11/2023
1.7.8 144 8/11/2023
1.7.7 141 8/10/2023
1.7.6 157 8/9/2023
1.7.5 133 8/9/2023
1.7.4 144 8/3/2023
1.7.3 150 8/1/2023
1.7.2 135 7/31/2023
1.7.1 136 7/27/2023
1.7.0 148 7/27/2023