public async Task HandleAsync(CreateOrder command) { var exists = await _repository.ExistsAsync(o => o.Id == command.OrderId); if (exists) { throw new InvalidOperationException($"Order with given id: {command.OrderId} already exists!"); } _logger.LogInformation($"Fetching a price for order with id: {command.OrderId}..."); var pricingDto = await _pricingServiceClient.GetAsync(command.OrderId); if (pricingDto is null) { throw new InvalidOperationException($"Pricing was not found for order: {command.OrderId}"); } _logger.LogInformation($"Order with id: {command.OrderId} will cost: {pricingDto.TotalAmount}$."); var order = new Order(command.OrderId, command.CustomerId, pricingDto.TotalAmount); await _repository.AddAsync(order); _logger.LogInformation($"Created an order with id: {command.OrderId}, customer: {command.CustomerId}."); var spanContext = _tracer.ActiveSpan.Context.ToString(); var @event = new OrderCreated(order.Id); if (_outbox.Enabled) { await _outbox.SendAsync(@event, spanContext : spanContext); return; } await _publisher.PublishAsync(@event, spanContext : spanContext); }
private async Task SendAsync(IEnumerable <ICommand> commands) { if (commands is null) { return; } var messageProperties = _messagePropertiesAccessor.MessageProperties; var originatedMessageId = messageProperties?.MessageId; var correlationId = _correlationIdFactory.Create(); var spanContext = messageProperties?.GetSpanContext(_spanContextHeader); if (string.IsNullOrWhiteSpace(spanContext)) { spanContext = _tracer.ActiveSpan is null ? string.Empty : _tracer.ActiveSpan.Context.ToString(); } var correlationContext = _contextAccessor.CorrelationContext ?? _httpContextAccessor.GetCorrelationContext(); var headers = new Dictionary <string, object>(); foreach (var @event in commands) { if (@event is null) { continue; } var messageId = Guid.NewGuid().ToString("N"); _logger.LogTrace($"Publishing integration event: {@event.GetType().Name.Underscore()} [ID: '{messageId}']."); if (_outbox.Enabled) { await _outbox.SendAsync(@event, originatedMessageId, messageId, correlationId, spanContext, correlationContext, headers); continue; } await _busPublisher.PublishAsync(@event, messageId, correlationId, spanContext, correlationContext, headers); } }
public async Task PublishAsync(IEnumerable <IEvent> events) { if (events is null) { return; } var messageProperties = _messagePropertiesAccessor.MessageProperties; var originatedMessageId = messageProperties?.MessageId; var correlationId = messageProperties?.CorrelationId; var spanContext = messageProperties?.GetSpanContext(_spanContextHeader); if (string.IsNullOrWhiteSpace(spanContext)) { spanContext = _tracer.ActiveSpan is null ? string.Empty : _tracer.ActiveSpan.Context.ToString(); } var headers = messageProperties.GetHeadersToForward(); var correlationContext = _contextAccessor.CorrelationContext ?? _httpContextAccessor.GetCorrelationContext(); foreach (var @event in events) { if (@event is null) { continue; } var messageId = Guid.NewGuid().ToString("N"); _logger.LogTrace($"Publishing integration event: {@event.GetType().Name} [id: '{messageId}']."); if (_outbox.Enabled) { await _outbox.SendAsync(@event, originatedMessageId, messageId, correlationId, spanContext, correlationContext, headers); continue; } await _busPublisher.PublishAsync(@event, messageId, correlationId, spanContext, correlationContext, headers); } }