protected override async Task ProcessMessageAsync(object message, string correlationId) { if (message == null) { throw new ArgumentNullException(nameof(message)); } await _executer.ExecuteAsync((ICommand)message, correlationId); }
public async Task SendAsync(Envelope <ICommand> envelope) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } await _executer.ExecuteAsync(envelope.Body, envelope.CorrelationId); }
public Task <object> ExecuteAsync <TResult>(ICommandExecuter commandExecuter, ICommand <TResult> command) { return(commandExecuter.ExecuteAsync(command).ContinueWith(m => { if (m.IsFaulted) { throw m.Exception; } return (object)m.Result; })); }
public async Task <TResult> ExecuteAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken = default(CancellationToken)) { _commandScopeManager.Enter(); try { await new SynchronizationContextRemover(); return(await _commandExecuter.ExecuteAsync(command, cancellationToken)); } finally { _commandScopeManager.Exit(); } }
public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken) { await new SynchronizationContextRemover(); ICommandDispatchContext dispatchContext = _commandScopeManager.Enter(); try { CommandResult <TResult> dispatchResult = null; CommandResult wrappedDispatchResult = null; ICommandExecuter executer = null; ICommandDispatcher dispatcher = null; ICommand unwrappedCommand = command; if (command is NoResultCommandWrapper wrappedCommand) { unwrappedCommand = wrappedCommand.Command; } // we specifically audit BEFORE dispatch as this allows us to capture intent and a replay to // occur even if dispatch fails // (there is also an audit opportunity after execution completes and I'm considering putting one in // on dispatch success) await _auditor.AuditPreDispatch(unwrappedCommand, dispatchContext, cancellationToken); try { Stopwatch stopwatch = _collectMetrics ? Stopwatch.StartNew() : null; Func <ICommandDispatcher> dispatcherFunc = _commandRegistry.GetCommandDispatcherFactory(command); if (dispatcherFunc != null) { dispatcher = dispatcherFunc(); if (command != unwrappedCommand) { wrappedDispatchResult = await dispatcher.DispatchAsync(unwrappedCommand, cancellationToken); } else { dispatchResult = await dispatcher.DispatchAsync(command, cancellationToken); } executer = dispatcher.AssociatedExecuter; } await _auditor.AuditPostDispatch(unwrappedCommand, dispatchContext, stopwatch?.ElapsedMilliseconds ?? -1, cancellationToken); if ((dispatchResult != null && dispatchResult.DeferExecution) || (wrappedDispatchResult != null && wrappedDispatchResult.DeferExecution)) { return(new CommandResult <TResult>(default(TResult), true)); } } catch (Exception ex) { throw new CommandDispatchException(unwrappedCommand, dispatchContext.Copy(), dispatcher?.GetType() ?? GetType(), "Error occurred during command dispatch", ex); } if (executer == null) { executer = AssociatedExecuter; } return(new CommandResult <TResult>(await executer.ExecuteAsync(command, cancellationToken), false)); } finally { _commandScopeManager.Exit(); } }