示例#1
0
        private string ConfigureQueue(IModel channel)
        {
            var exchange   = _exchangeNameFactory.Build <T>();
            var routingKey = _routingKeyFactory.Build <T>();
            var queue      = _queueNameFactory.Build <T>();

            var basicProperties = channel.CreateBasicProperties();

            basicProperties.ContentType = _messageFormatter.GetMIMEContentType();

            channel.QueueDeclare(queue, _configuration.IsQueueDurable, _configuration.IsQueueExclusive, _configuration.SupportsAutoDelete, null);
            channel.QueueBind(queue, exchange, routingKey);

            return(queue);
        }
        public void Send <T>(T message)
        {
            var connectionFactory = new ConnectionFactory {
                Uri = _uri
            };

            using (var connection = connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    var exchange    = _exchangeNameFactory.Build <T>();
                    var routingKey  = _routingKeyFactory.Build <T>();
                    var messageBody = _messageFormatter.Write(message);

                    var basicProperties = channel.CreateBasicProperties();
                    basicProperties.ContentType = _messageFormatter.GetMIMEContentType();

                    channel.ExchangeDeclare(exchange, _configuration.ExchangeType, _configuration.IsExchangeDurable);

                    channel.BasicPublish(exchange, routingKey, basicProperties, messageBody);

                    channel.Close();
                    connection.Close();
                }
        }