Skip to content

Add DispatchConsumersAsync to IConnectionFactory #380

@paulomorgado

Description

@paulomorgado

If I receive an IConnectionFactory instance in my component, I have no way to know if it has DispatchConsumersAsync set to true or false.

public MyComponent(IConnectionFactory connectionFactory, string queueName)
{
    var connection = connectionFactory.CreateConnection();
    var model = connection.CreateModel();

    var consumer = new AsyncEventingBasicConsumer(this.model);
    consumer.Received += (s, e) => this.HandleRequestAsync(s, e);

    this.model.BasicConsume(
        queue: queueName,
        autoAck: true,
        consumerTag: string.Empty,
        noLocal: false,
        exclusive: false,
        arguments: null,
        consumer: consumer);
}

There's no way to know if this is going to work or not.

Assuming that it might be a bad idea to have a setter for DispatchConsumersAsync in IConnectionFactory, at least a getter would be nice.

public MyComponent(IConnectionFactory connectionFactory, string queueName)
{
    var connection = connectionFactory.CreateConnection();
    var model = connection.CreateModel();

    IBasicConsumer consumer:

    if (connectionFactory.DispatchConsumersAsync) // or connection.DispatchConsumersAsync
    {
        consumer = new AsyncEventingBasicConsumer(this.model);
        consumer.Received += this.HandleRequestAsync;
    }
    else
    {
        consumer = new EventingBasicConsumer(this.model);
        consumer.Received += (s, e) => this.HandleRequestAsync(s, e).Wait();
    }

    this.model.BasicConsume(
        queue: queueName,
        autoAck: true,
        consumerTag: string.Empty,
        noLocal: false,
        exclusive: false,
        arguments: null,
        consumer: consumer);
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions