Search code examples
rabbitmqmasstransit

Is there support for the RabbitMQ mandatory flag in MassTransit


I would like to get an exception when sending to a RabbitMQ exchange using MassTransit if the message is not delivered to any queues. It looks like the RabbitMQ mandatory flag could be used for something like this, but I have not been able to find any way to set this using MassTransit. Is this supported?

My code:

var personSendEndpoint = busControl.GetSendEndpoint(new Uri($"rabbitmq://localhost/{personQueueName}")).Result;

// An exception should occur on the following call if the message is not delivered to any queue
personSendEndpoint.Send(new PersonUpdated() { Name = "Dorian Gray", Key = Guid.NewGuid() });

Solution

  • You can set the Mandatory flag, but there isn't a cute extension methods to do it for you. Basically, you do as shown below:

    await endpoint.Send(new PersonUpdate(...), context =>
    {
        (context as RabbitMqSendContext).Mandatory = true;
    });
    

    And that will set the flag telling RabbitMQ to throw an exception if no bindings for the exchange exist.