/// <summary> /// Awaitably logs the command we received to the command store. /// </summary> /// <param name="command">The command that we want to store.</param> /// <param name="cancellationToken">Allows the caller to cancel the pipeline if desired</param> /// <returns>The parameter to allow request handlers to be chained together in a pipeline</returns> public override async Task <T> HandleAsync(T command, CancellationToken cancellationToken = default(CancellationToken)) { if (_onceOnly) { _logger.Value.DebugFormat("Checking if command {0} has already been seen", command.Id); //TODO: We should not use an infinite timeout here - how to configure var exists = await _commandStore.ExistsAsync <T>(command.Id, _contextKey, -1, cancellationToken).ConfigureAwait(ContinueOnCapturedContext); if (exists && _onceOnlyAction is OnceOnlyAction.Throw) { _logger.Value.DebugFormat("Command {0} has already been seen", command.Id); throw new OnceOnlyException($"A command with id {command.Id} has already been handled"); } if (exists && _onceOnlyAction is OnceOnlyAction.Warn) { _logger.Value.WarnFormat("Command {0} has already been seen", command.Id); return(command); } } _logger.Value.DebugFormat("Writing command {0} to the Command Store", command.Id); T handledCommand = await base.HandleAsync(command, cancellationToken).ConfigureAwait(ContinueOnCapturedContext); //TODO: We should not use an infinite timeout here - how to configure await _commandStore.AddAsync(command, _contextKey, -1, cancellationToken).ConfigureAwait(ContinueOnCapturedContext); return(handledCommand); }
public async Task Command_Is_Not_Stored_If_The_Handler_Is_Not_Succesful() { Guid id = Guid.NewGuid(); Catch.Exception(() => _commandProcessor.Send(new MyCommandToFail() { Id = id })); _commandStore.ExistsAsync <MyCommandToFail>(id, typeof(MyStoredCommandToFailHandlerAsync).FullName).Result.Should().BeFalse(); }