Ejemplo n.º 1
0
        /// <inheritdoc />
        public void Publish(T message)
        {
            string messageConentType;
            var    messageString = OnSerializeMessage(message, out messageConentType);

            var msg = new RabbitMqMessageRequest
            {
                ContentData = messageString,
                ContentType = messageConentType
            };

            using (var connection = OnCreateConnection())
            {
                OnPublishing(msg, connection);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Publishes a message on the exchange
        /// </summary>
        /// <param name="message"></param>
        /// <param name="connection"></param>
        protected virtual void OnPublishing(RabbitMqMessageRequest message, IConnection connection)
        {
            var exchange = Settings.ExchangeName;
            var queue    = Settings.QueueName;
            var routing  = Settings.RoutingKey;

            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange, ExchangeType.Direct);
                channel.QueueDeclare(queue, true, false, false, null);
                channel.QueueBind(queue, exchange, routing, null);

                var props = channel.CreateBasicProperties();
                props.ContentEncoding = Encoding.UTF8.HeaderName;
                props.ContentType     = message.ContentType;
                props.DeliveryMode    = 2;

                var messageBodyBytes = Encoding.UTF8.GetBytes(message.ContentData);

                channel.BasicPublish(exchange, routing, props, messageBodyBytes);
            }
        }