在Rabbitmq-dotnet-client中将“ consumer_cancel_notify”设置为“ false”的ClientProperties传递

问题描述 投票:0回答:1

我使用基于rabbitmq-dotnet-client的异步Rabbitmq使用者。

我希望RabbitMQ服务器不发送ACK来响应BasicCancel消息。

根据documentation,我可以在建立连接的consumer_cancel_notify属性中将参数falseClientProperties值一起传递。

我尝试使用此类代码来实现。

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代理?

c# rabbitmq
1个回答
0
投票

您正在以错误的方式进行操作。您的消费者应该看起来像这样(基本版本)

        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);
© www.soinside.com 2019 - 2024. All rights reserved.