Una simple implementación de C#/. Net Net Distributed Lease/Lock Manager (DLM).
Inspirado en https://github.com/fbeltrao/azfunctions-distributed-locking
Instale una de las implementaciones de almacenamiento de arrendamiento siguiendo sus instrucciones README:
Dentro de su controlador/servicio, inyecte el IDistributedLeaseManager y llame al método TryAcquireLease . Verifique si el resultado fue exitoso: si fue entonces, puede continuar con la operación; De lo contrario, alguien más ha adquirido el contrato de arrendamiento:
await using var leaseResult = await leaseManager . TryAcquireLease ( resourceId , TimeSpan . FromSeconds ( 5 ) ) ;
if ( ! leaseResult . IsSuccessful )
{
// Someone else has required the lease for the resource.
// You may want to either retry the acqusition or abort the operation.
}
else
{
// You are the lease owner now and can safely process the resource.
// The lease will be released either when the leaseResult gets disposed
// or when the lease expires (in the example above, in 5 seconds)
} Si desea utilizar un almacenamiento de arrendamiento diferente del proporcionado por el autor, agregue la biblioteca DistributedLeaseManager.Core a su proyecto e implementa la interfaz correspondiente (consulte cualquiera de las implementaciones existentes como un ejemplo).
¡Siéntase libre de abrir un PR con sus cambios para incluirlos en el paquete!