Search code examples
c#rabbitmqamqp

Passing ClientProperties with "consumer_cancel_notify" set to "false" in rabbitmq-dotnet-client


I use async rabbitmq consumer based on rabbitmq-dotnet-client.

Here is a simplify code

using (_channel = RabbitMqConnectionFactory.Connection.CreateModel())
{
    _channel.QueueDeclare(Constants.QueueName,
        durable: true,
        exclusive: false,
        autoDelete: false,
        arguments: null);

    _channel.BasicQos(0, 1, false);

    var consumer = new AsyncEventingBasicConsumer(_channel);

    consumer.Received += async (o, a) =>
    {
        await HandleMessageEvent(o, a);
    };

    string tag = _channel.BasicConsume(Constants.QueueName, false, consumer);

    while (IsWorking)
    {
        await Task.Delay(6000);
    }

    _channel.BasicCancel(tag);

    IsWorking = false;
}

I want RabbitMQ server not to send an ACK in response to a BasicCancel message.

According to the documentation , I can pass the parameter consumer_cancel_notify with the false value in the ClientProperties property of the connection when it's established.

I try to do so with such code.

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;
}

But, this does not work, since the server still sends the ACK to the BasicCancel message, which i can handle with ConsumerCancelled AsyncEventHandled.

I use RabbitMQ Server version 3.8.3 and rabbitmq-dotnet-client version 5.1.2.

How can i pass the consumer_cancel_notify parameter to the RabbitMQ broker?


Solution

  • In version 6.0.0 of the RabbitMQ .NET client, a method void BasicCancelNoWait (string consumerTag) has been added to the public API.

    When using this method on the client side, the server does not send the ACK in response to a BasicCancel request.