コード例 #1
0
        public void Publish(Type messageType, EventBusMessage message)
        {
            _eventBusConnection.EnsureConnection();

            var eventName = _manager.GetEventName(messageType);

            _logger.LogTrace($"Creating RabbitMQ channel to publish event: {message.MessageId} ({eventName})");

            using (var channel = _eventBusConnection.CreateModel())
            {
                _logger.LogTrace($"Declaring RabbitMQ exchange to publish event: {message.MessageId}");

                var messageJson = JsonConvert.SerializeObject(message);
                var body        = Encoding.UTF8.GetBytes(messageJson);

                createRetryPolicy(eventName, message.MessageId).Execute(() =>
                {
                    var properties          = channel.CreateBasicProperties();
                    properties.DeliveryMode = 2; // persistent

                    _logger.LogTrace($"Publishing event to RabbitMQ: {message.MessageId}");

                    channel.BasicPublish(
                        exchange: _exchangeName,
                        eventName,
                        mandatory: true,
                        basicProperties: properties,
                        body: body);
                });
            }
        }