public async Task CommitAsync(Func <Task> next) { var clientOutboxTransaction = _unitOfWorkContext.Get <IClientOutboxTransaction>(); var transportOperations = _unitOfWorkContext.GetEvents().Select(e => new TransportOperation(GuidComb.NewGuidComb(), e)).ToList(); var clientOutboxMessage = transportOperations.Any() ? new ClientOutboxMessageV2(GuidComb.NewGuidComb(), _settings.EndpointName(), transportOperations) : null; if (clientOutboxMessage != null) { await _clientOutboxStorage.StoreAsync(clientOutboxMessage, clientOutboxTransaction).ConfigureAwait(false); } await next().ConfigureAwait(false); if (clientOutboxMessage != null) { var tasks = clientOutboxMessage.TransportOperations.Select(o => { if (o.Message.IsCommand()) { return(SendMessage(o)); } else { return(PublishMessage(o)); } }); await Task.WhenAll(tasks).ConfigureAwait(false); await _clientOutboxStorage.SetAsDispatchedAsync(clientOutboxMessage.MessageId, clientOutboxTransaction).ConfigureAwait(false); } }
public async Task Handle(ProcessClientOutboxMessageCommandV2 message, IMessageHandlerContext context) { var clientOutboxMessageId = Guid.Parse(context.MessageId); var clientOutboxMessage = await _clientOutboxStorage.GetAsync(clientOutboxMessageId, context.SynchronizedStorageSession); var tasks = clientOutboxMessage.TransportOperations.Select(o => { var publishOptions = new PublishOptions(); publishOptions.SetMessageId(o.MessageId.ToString()); return(context.Publish(o.Message, publishOptions)); }); await Task.WhenAll(tasks); await _clientOutboxStorage.SetAsDispatchedAsync(clientOutboxMessage.MessageId, context.SynchronizedStorageSession); }