Does the RabbitMQ .NET client have any sort of asynchronous support? I'd like to be able to connect and consume messages asynchronously, but haven't found a way to do either so far.
(For consuming messages I can use the EventingBasicConsumer, but that's not a complete solution.)
Just to give some context, this is an example of how I'm working with RabbitMQ at the moment (code taken from my blog):
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("testqueue", true, false, false, null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += Consumer_Received;
channel.BasicConsume("testqueue", true, consumer);
Console.ReadLine();
}
}
Rabbit supports dispatching to asynchronous message handlers using the
AsyncEventingBasicConsumer
class. It works similarly to theEventingBasicConsumer
, but allows you to register an callback which returns aTask
. The callback is dispatched to and the returnedTask
is awaited by the RabbitMQ client.there is no async/await support built in to the RabbitMQ .NET client at this point. There is an open ticket for this on the RabbitMQ .NET Client repository
To summarize current
async
/TPL
support:AsyncEventingBasicConsumer
which you can register events for and return aTask
.AsyncDefaultBasicConsumer
for which you can override virtual methods such asHandleBasicDeliver
and return aTask
. Original PR here (looks like it was also introduced in 5.0?)async
operations, but I don't see any specific links to that effort.