Can't use Bind and ConfigureConsumer together

Viewed 972

I'm using MassTransit + RabbitMQ combo in Asp.Net Core app. The relevant configuration part below:

public IBusControl CreateBus(IServiceProvider serviceProvider)
{
    var options = serviceProvider.GetService<IConfiguration>().GetOptions<RabbitMqOptions>("rabbitmq");

    return Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        cfg.Host($"rabbitmq://{options.Host}:{options.Port}");
        cfg.ReceiveEndpoint("ingest-products", ep =>
        {
            ep.PrefetchCount = 16;

            ep.UseMessageRetry(r => r.Interval(2, 1000));
            ep.Bind<CreateProducts>(x =>
            {
                x.RoutingKey = "marketplace";
                x.ExchangeType = ExchangeType.Direct;
                x.AutoDelete = false;
                x.Durable = true;
            });

            ep.ConfigureConsumer<CreateProductsConsumer>(serviceProvider);
        });
    });
}

When I run the application, I'm getting this exception:

ArgumentException: The MassTransit.RabbitMqTransport.Topology.Entities.ExchangeEntity entity settings did not match the existing entity

What am I doing wrong here? Am I not supposed to configure a consumer with the IServiceProvider after I bind exchange to a receive endpoint? If not, then how do I configure it properly (well, I still want stuff to get injected into my consumers)?

3 Answers

If you are binding the messages types to the receive endpoint that are the same as message types in the consumer, you need to disable the automatic exchange binding.

// for MassTransit versions v6 and earlier
endpoint.BindMessageExchanges = false;

// for MassTransit versions 7 and onward
endpoint.ConfigureConsumeTopology = false;

This will prevent MassTransit from trying to bind the messages types of the consumer on the endpoint.

I spent most of my day trying to find this. In v7+ it was renamed to:

endpoint.ConfigureConsumeTopology = false;

You need to disable automatic message exchange binding in order for your custom message binding to work.

endpoint.ConfigureConsumeTopology = false;

By following the source code on GitHub we can see that ConfigureConsumeTopology method obsoletes the previous methods, such as BindMessageTopics, BindMessageExchanges, SubscribeMessageTopics.

Related