MarkHasper
MarkHasper

Reputation: 187

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() });

Upvotes: 1

Views: 652

Answers (2)

Ykok
Ykok

Reputation: 1415

Just wanted to add to the nice answer by @ChrisPatterson. You can do this as part of your configuration (part of AddMassTransit method - c is of type IBusRegistrationConfigurator):

c.UsingRabbitMq((context, configurator) =>
{
    configurator.ConfigureEndpoints(context);

    configurator.ConfigureSend(sendPipeConfigurator =>
    {
        sendPipeConfigurator.AddPipeSpecification(new DelegatePipeSpecification<SendContext>(sendContext =>
        {
            if (sendContext is RabbitMqSendContext rabbitSendContext)
            {
                rabbitSendContext.Mandatory = true;
            }
        }));
    });
});

Upvotes: 0

Chris Patterson
Chris Patterson

Reputation: 33457

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.

Upvotes: 1

Related Questions