It’s quite common to do some sort of I/O operation (e.g. REST call) whenever a message is consumed by a RabbitMQ client. This should be done asynchronously, but it’s not as simple as changing the event handling code to async void .

In “The Dangers of async void Event Handlers“, I explained how making an event handler async void will mess up the message order, because the dispatcher loop will not wait for a message to be fully processed before calling the handler on the next one.

While that article provided a workaround that is great to use with older versions of the RabbitMQ Client library, it turns out that there is an AsyncEventingBasicConsumer as from RabbitMQ.Client 5.0.0-pre3 which works great for asynchronous message consumption.

AsyncEventingBasicConsumer Example

First, we need to make sure that the RabbitMQ client library is installed.

Install-Package RabbitMQ.Client

Then, we can set up a publisher and consumer to show how to use the AsyncEventingBasicConsumer . Since this is just a demonstration, we can have both in the same process:

static void Main(string[] args) { var factory = new ConnectionFactory() { DispatchConsumersAsync = true }; const string queueName = "myqueue"; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queueName, true, false, false, null); // consumer var consumer = new AsyncEventingBasicConsumer(channel); consumer.Received += Consumer_Received; channel.BasicConsume(queueName, true, consumer); // publisher var props = channel.CreateBasicProperties(); int i = 0; while (true) { var messageBody = Encoding.UTF8.GetBytes($"Message {++i}"); channel.BasicPublish("", queueName, props, messageBody); Thread.Sleep(50); } } }

There is nothing really special about the above code except that we’re using AsyncEventingBasicConsumer instead of EventingBasicConsumer , and that the ConnectionFactory is now being set up with a suspicious-looking DispatchConsumersAsync property set to true . The ConnectionFactory is using defaults, so it will connect to localhost using the guest account.

The message handler is expected to return Task , and this makes it very easy to use proper asynchronous code:

private static async Task Consumer_Received(object sender, BasicDeliverEventArgs @event) { var message = Encoding.UTF8.GetString(@event.Body); Console.WriteLine($"Begin processing {message}"); await Task.Delay(250); Console.WriteLine($"End processing {message}"); }

The messages are indeed processed in order:

How to Mess This Up

Remember that DispatchConsumersAsync property? I haven’t really found any documentation explaining what it actually does, but we can venture a guess after a couple of experiments.

First, let’s keep that property, but use a synchronous EventingBasicConsumer instead (which also means changing the event handler to have a void return type). When we run this, we get an error:

It says “In the async mode you have to use an async consumer”. Which I suppose is fair enough.

So now, let’s go back to using an AsyncEventingBasicConsumer , but leave out the DispatchConsumersAsync property:

var factory = new ConnectionFactory();

This time, you’ll see that the the event handler is not firing (nothing is being written to the console). The messages are indeed being published, and the queue is remaining at zero messages, so they are being consumed (you’ll see them accumulate if you disable the consumer).

This is actually quite dangerous, yet there is no error like the one we saw earlier. It means that if a developer forgets to set that DispatchConsumersAsync property, then all messages are lost. It’s also quite strange that the choice of how to dispatch messages to the consumer (i.e. sync or async) is a property of the connection rather than the consumer, although presumably it would be a result of some internal plumbing in the RabbitMQ Client library.

Summary

AsyncEventingBasicConsumer is great for having pure asynchronous RabbitMQ consumers, but don’t forget that DispatchConsumersAsync property.

It’s only available since RabbitMQ.Client 5.0.0-pre3, so if you’re on an older version, use the workaround described in “The Dangers of async void Event Handlers” instead.