public async Task Handle(T command, CancellationToken cancellationToken = default) { if (command is ILockIdentifier identifier) { await _lockProvider.Start(); try { var lockId = identifier.LockId; try { if (!await _lockProvider.AcquireLock(lockId, cancellationToken)) { throw new EntityLockedException($"Unable to handle command '{command.GetType().FullName}'. The entity with with identifier '{lockId}' is already handling a command."); } await _handler.Handle(command, cancellationToken); } finally { await _lockProvider.ReleaseLock(lockId); } } finally { await _lockProvider.Stop(); } } else { await _handler.Handle(command, cancellationToken); } }
public async Task HandleAsync(SendSmsCommand command, CancellationToken cancellationToken = default(CancellationToken)) { await _lockProvider.Start(); try { var conversation = command.Message.Conversation.ToConversation(); var lockId = conversation.Id.ToString(); try { if (!await _lockProvider.AcquireLock(lockId, cancellationToken)) { throw new ConversationLockedException($"A message for conversation {lockId} is already being processed."); } await _handler.HandleAsync(command); } finally { await _lockProvider.ReleaseLock(lockId); } } finally { await _lockProvider.Stop(); } }
public void Setup() { Subject = CreateProvider(); Subject.Start(); }