private async Task PersistEvents() { await CommandsEventsStore.PersistEvents(Events); Events.Clear(); }
public async Task Handle <T>(T command) where T : Command { var commandHandlingMetric = MetricService.StartForType(command, "CommandHandling"); try { if (command == null) { throw new CommandCannotBeNullException(typeof(T).Name); } if (command.PublishedUTC != default) { Time.OverrideTime(command.PublishedUTC); } SetCorrelationProperties(command); await ValidateCommand(command); var commandKey = command.GetType(); if (!Registry.HasCommandHanlder(commandKey)) { throw new MissingCommandHandlerException(commandKey.Name); } try { await CommandsEventsStore.PersistCommand(command); var metric = MetricService.StartForType(command, "CommandProcessing"); await Registry.GetCommandHandler(commandKey).Handle(command, ServiceProvider); metric.Stop(); await PersistEvents(); } catch (TargetInvocationException ex) { await Publish(new CommandExecutionFailed(ex.InnerException ?? ex, command), command); throw; } catch (Exception ex) { await Publish(new CommandExecutionFailed(ex, command), command); throw; } finally { await PersistEvents(); } } finally { commandHandlingMetric.Stop(); } }