private void OnReceived(Object sender, BasicDeliverEventArgs e) { try { var message = _rabbitMqMessageFactory.Create(e); var eventMetadata = new EventMetadata(message.Headers.ToDictionary(kv => kv.Key, kv => Encoding.UTF8.GetString((Byte[])kv.Value))); var eventDefinition = _applicationDefinition.Events.GetDefinition(eventMetadata.EventName, eventMetadata.EventVersion); if (eventDefinition == null) { _channel.BasicAck(e.DeliveryTag, false); return; } var aggregateEvent = (IAggregateEvent)JsonConvert.DeserializeObject(message.Message, eventDefinition.Type); var domainEvent = _domainEventFactory.Create(aggregateEvent, eventMetadata); _domainEventBus.Publish(domainEvent); } catch { if (_connection.IsOpen) { _channel.BasicNack(e.DeliveryTag, false, false); } } if (_connection.IsOpen) { _channel.BasicAck(e.DeliveryTag, false); } }
public async Task Publish(IDomainEvent domainEvent) { var connection = await GetConnection(); using (var channel = connection.CreateModel()) { await Initialize(channel); var message = _rabbitMqMessageFactory.Create(domainEvent); var properties = channel.CreateBasicProperties(); properties.Headers = message.Headers.ToDictionary(kv => kv.Key, kv => kv.Value); properties.Timestamp = new AmqpTimestamp(DateTimeOffset.Now.ToUnixTime()); properties.ContentEncoding = "utf-8"; properties.ContentType = "application/json"; properties.MessageId = message.MessageId; channel.ConfirmSelect(); channel.BasicPublish(_configuration.ExchangeName, "", false, properties, Encoding.UTF8.GetBytes(message.Message)); channel.WaitForConfirms(); } }