目前遇到一个RabbitMQ无法接收消息的问题,问题如下:
发送端代码:
var factory = new ConnectionFactory();
factory.HostName = "localhost";
factory.UserName = "station";
factory.Password = "123456-a";
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.QueueDeclare("20-x:device-message", true, false, false, null);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "20-x:device-message", properties, body);
}
}
消息发送成功。
接收端代码:
var factory = new ConnectionFactory();
factory.HostName = "localhost";
factory.UserName = "station";
factory.Password = "123456-a";
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var messageText = Encoding.UTF8.GetString(body);
//var message = JsonConvert.DeserializeObject<ICreateMessageDto>(messageText);
//channel.BasicAck(ea.DeliveryTag, true);
};
channel.BasicConsume("20-x:device-message", false, consumer);
}
}
var messageText = Encoding.UTF8.GetString(body);
这行代码一直无法触发。
RabbitMQ 后台管理界面,看看有没有消费者
消息中心配置了?
接收不到是正常的,消费者监听后信道立即关闭(看using代码),还没来得及等待消息推过来。