消费者“收到"事件未触发 [英] Consumer "received" event not firing
问题描述
我正在尝试设置对 RabbitMQ 队列的订阅,并将自定义事件处理程序传递给它.所以我有一个名为 RabbitMQClient
的类,它包含以下方法:
I'm trying to set up a subscription to a RabbitMQ queue and pass it a custom event handler.
So I have a class called RabbitMQClient
which contains the following method:
public void Subscribe(string queueName, EventHandler<BasicDeliverEventArgs> receivedHandler)
{
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(
queue: queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null
);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += receivedHandler;
channel.BasicConsume(
queue: queueName,
autoAck: false,
consumer: consumer
);
}
}
}
我正在使用依赖注入,所以我有一个 RabbitMQClient
(单例)接口.
I'm using dependency injection, so I have a RabbitMQClient
(singleton) interface for it.
在我的消费类中,我有这个方法,我想充当 EventHandler
In my consuming class, I have this method which I want to act as the EventHandler
public void Consumer_Received(object sender, BasicDeliverEventArgs e)
{
var message = e.Body.FromByteArray<ProgressQueueMessage>();
}
我正在尝试像这样订阅队列:
And I'm trying to subscribe to the queue like this:
rabbitMQClient.Subscribe(Consts.RabbitMQ.ProgressQueue, Consumer_Received);
我可以看到队列开始接收消息,但 Consumer_Received
方法没有触发.
I can see that the queue starts to get messages, but the Consumer_Received
method is not firing.
我在这里遗漏了什么?
推荐答案
使用"调用 dispose 在您的连接上,您的事件不会被触发.只需从代码中删除您的使用"块,这样它就不会关闭连接.
"Using" calls dispose on your connection and your event wont be triggered. Just remove your "using" block from the code so that it doesn't close the connection.
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.QueueDeclare(
queue: queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += receivedHandler;
channel.BasicConsume(
queue: queueName,
autoAck: false,
consumer: consumer);
这篇关于消费者“收到"事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!