我使用基于rabbitmq-dotnet-client的异步Rabbitmq使用者。
我希望RabbitMQ服务器不发送ACK来响应BasicCancel
消息。
根据documentation,我可以在建立连接的consumer_cancel_notify
属性中将参数false
与ClientProperties
值一起传递。
我尝试使用此类代码来实现。
public static ConnectionFactory GetRabbitMqConnectionFactory()
{
Dictionary<string, bool> capabilities = new Dictionary<string, bool>
{
["consumer_cancel_notify"] = false
};
var result = new ConnectionFactory
{
ContinuationTimeout = TimeSpan.FromSeconds(5),
HostName = "localhost",
UserName = "guest",
Password = "guest",
DispatchConsumersAsync = true,
ClientProperties =
{
["capabilities"] = capabilities
},
};
return result;
}
但是,这不起作用,因为服务器仍然将ACK发送到BasicCancel
消息,我可以使用ConsumerCancelled
AsyncEventHandled处理该消息。
我使用RabbitMQ Server版本3.8.3和rabbitmq-dotnet-client版本5.1.2。
如何将consumer_cancel_notify
参数传递给RabbitMQ代理?
您正在以错误的方式进行操作。您的消费者应该看起来像这样(基本版本)
consumer = new EventingBasicConsumer(Channel);
consumer.Received += (sender, basicDeliveryEventArgs) =>
{
var payload = basicDeliveryEventArgs.Body;
.... //do your stuff
Channel.BasicAck(basicDeliveryEventArgs.DeliveryTag, false); //Success
... or on failure
Channel.BasicNack(basicDeliveryEventArgs.DeliveryTag, false); //Failed
};
consumerTag = Channel.BasicConsume(queue: definitions.QueueName,
autoAck: false,
consumer: consumer);