public async Task <IAsyncDisposable> AcquireAsync(string lockName) { if (string.IsNullOrEmpty(lockName)) { throw new ArgumentNullException(nameof(lockName)); } if (_cosmosDistributedLockClaim != null) { throw new DistributedLockAlreadyAcquiredException(lockName); } try { _cosmosDistributedLockClaim = new CosmosDistributedLockClaim(lockName); var result = await _cosmosDistributedLockStore .TryClaimLockAsync(_cosmosDistributedLockClaim) .ConfigureAwait(false); if (result) { return(this); } _cosmosDistributedLockClaim = null; throw new DistributedLockNotAcquiredException(lockName, _options.Value.DistributedLocksCollection, null); } catch (Exception ex) when(!(ex is DistributedLockNotAcquiredException)) { throw new DistributedLockNotAcquiredException(lockName, _options.Value.DistributedLocksCollection, ex); } }
public async ValueTask DisposeAsync() { if (string.IsNullOrEmpty(_cosmosDistributedLockClaim?.Id)) { return; } await _cosmosDistributedLockStore .ReleaseLockAsync(_cosmosDistributedLockClaim) .ConfigureAwait(false); _cosmosDistributedLockClaim = null; }