Getting Started with EricksonLopez.DistributedLock
L2StableNuGet: v1.0.0Local HEAD: v2.1.0AOT: Full
Install EricksonLopez.DistributedLock.Abstractions (1.0.0 on NuGet) together with your chosen provider package (there is no root EricksonLopez.DistributedLock metapackage).
dotnet add package EricksonLopez.DistributedLock.Abstractions --version 1.0.0<PackageReference Include="EricksonLopez.DistributedLock.Abstractions" Version="1.0.0" /><PackageVersion Include="EricksonLopez.DistributedLock.Abstractions" Version="1.0.0" />dotnet add package EricksonLopez.DistributedLock.Sqlite --version 1.0.0# Or choose .PostgreSql, .SqlServer, .MySql, .MariaDb, .Oracle, .RedisQuick Start: ExecuteWithLockAsync with SQLite
Section titled “Quick Start: ExecuteWithLockAsync with SQLite”Provider DI registration extensions (AddSqliteDistributedLock, AddPostgresDistributedLock, etc.) live in namespace Microsoft.Extensions.DependencyInjection;:
using System;using System.Threading;using System.Threading.Tasks;using EricksonLopez.DistributedLock.Abstractions;using EricksonLopez.Result;using Microsoft.Extensions.DependencyInjection;
namespace Example.DistributedLockDocs;
public static class DistributedLockQuickStart{ public static async Task<Result<string>> RunLockedWorkAsync(CancellationToken ct = default) { var services = new ServiceCollection(); services.AddLogging(); services.AddSqliteDistributedLock("Data Source=locks.db");
await using var sp = services.BuildServiceProvider(); var lockProvider = sp.GetRequiredService<IDistributedLockProvider>();
return await lockProvider.ExecuteWithLockAsync( resourceId: "billing-run:2026-12", timeout: TimeSpan.FromSeconds(5), action: static async linkedCt => { linkedCt.ThrowIfCancellationRequested(); await Task.CompletedTask; return "Completed"; }, cancellationToken: ct); }}