我使用 EasyNetQ 实现了一个简单的 RabbitMQ 客户端。连接后,我收到通知"Model shutdown for queue"。这是我的代码:
var _bus = RabbitHutch.CreateBus(String.Format("host={0}", hostName)).Advanced;
var _exchange = Exchange.DeclareFanout(exName);
var _queue = Queue.DeclareTransient();
_queue.BindTo(_exchange, "_");
_bus.Subscribe(
_queue,
(msg, properties, messageReceivedInfo) =>
{
return Task.Factory.StartNew(() =>
{
Console.WriteLine(msg.Length.ToString());
});
});
使用更底层的方法,一切都很好(消息长度显示在控制台中):
var factory = new ConnectionFactory();
factory.HostName = hostName;
var connect = factory.CreateConnection();
var channel = connect.CreateModel();
channel.ExchangeDeclare(exName, "fanout");
var resultQueue = channel.QueueDeclare(string.Empty, false, true, false, null);
string queueName = resultQueue.QueueName;
var consumer = new QueueingBasicConsumer(channel);
channel.QueueBind(queueName, exName, string.Empty);
var resultConsume = channel.BasicConsume(queueName, false, consumer);
while(true)
{
var e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
Console.WriteLine(e.Body.Length.ToString());
channel.BasicAck(e.DeliveryTag, false);
}
请提示,第一种方法有什么问题?
UPD 我用 IntelliTrace 捕获了异常:
AMQP 操作被中断:AMQP 关闭原因,由 Peer 发起,code=406,text="PRECONDITION_FAILED - 无法在 vhost '/' 中以不同类型、持久、内部或自动删除值重新声明交换 'live'",classId=40 , 方法 ID=10, 原因=
交换设置相同(见上文)。那么有什么问题呢?