Exception handler for consumers exceptions in C# client.

988 views
Skip to first unread message

AM

unread,
Feb 2, 2016, 5:11:27 AM2/2/16
to rabbitmq-users
Hi!

Is it possible to register an handler for exceptions that may be thronw in a Consumer?

I've got a simple consumer, like the following:

using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

class Worker
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "task_queue",
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                PerformSomethingThatMayThrowAnException(Encoding.UTF8.GetString(ea.Body));
                
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };
            channel.BasicConsume(queue: "task_queue",
                                 noAck: false,
                                 consumer: consumer);

            Console.ReadLine();
        }
    }
}

where consumer.Received may thrown an exception (for example in the PerformSomethingThatMayThrowAnException method).
I would love to avoid the use of a catch, and rely instead on some exception handler to setup.

I googled a lot and found something for the Java client, but I did not find any information for the C# client.
Is it possible to register a handler and setup a method to be called whenever an unhandled exception is thrown in the Consumer?

Thank you.
AM

Michael Klishin

unread,
Feb 2, 2016, 6:54:00 AM2/2/16
to rabbitm...@googlegroups.com, AM
On 2 February 2016 at 13:11:29, AM (arialdo...@gmail.com) wrote:
> Is it possible to register an handler for exceptions that may
> be thronw in a Consumer?

Not in .NET client at the moment but we'll get to adding unhandled exception handlers
in a future 3.6.x release:
https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/132 
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Reply all
Reply to author
Forward
0 new messages