Search code examples
c#asp.netrabbitmqmasstransit

ASP.NET Core service not creating RabbitMQ queue


Being a new user of MassTransit and RabbitMQ I'm currently trying to make my ASP.NET core service to work with MassTransit.

Taking this documentation section to configure MassTransit and ASP.NET Core I'm unable to get it working.

Currently (part of) the Startup.cs looks like

 services.AddMassTransit(x =>
            {
                x.AddConsumer<MailConsumer>();
                x.AddConsumer<MailFailedConsumer>();

                x.AddBus(provider => ConfigureBus(provider, rabbitMqConfigurations));
            });


private IBusControl ConfigureBus(
                            IServiceProvider provider,
                            RabbitMqConfigSection rabbitMqConfigurations) => Bus.Factory.CreateUsingRabbitMq(
                            cfg =>
                            {
                                var host = cfg.Host(
                                    rabbitMqConfigurations.Host,
                                    "/",
                                    hst =>
                                    {
                                        hst.Username(rabbitMqConfigurations.Username);
                                        hst.Password(rabbitMqConfigurations.Password);
                                    });

                                cfg.ReceiveEndpoint(host, $"{typeof(MailSent).Namespace}.{typeof(MailSent).Name}", endpoint =>
                                {
                                    endpoint.Consumer<MailConsumer>(provider);
                                });

                                cfg.ReceiveEndpoint(host, $"{typeof(MailSentFailed).Namespace}.{typeof(MailSentFailed).Name}", endpoint =>
                                {
                                    endpoint.Consumer<MailFailedConsumer>(provider);
                                });
                            });

The exchange is created automatically in RabbitMQ on startup, but no queue is bind to the exchange which I would expect.

After invoking my API endpoint I can see activity on the exchange, but of course the consumers doing nothing as there is no queue.

What (obvious) part am I missing?


Solution

  • Ok, I found the issue. It worked as described in the docs at the moment the docs were written. There are several AddMassTransit extensions for the IServiceCollection interface, which is confusing.

    AddMassTransit overload, which accepts the bus instance works as described.

    AddMassTransit overload, which accepts the Action<IServiceCollectionConfigurator> only does necessary registrations.

    You need to add one line:

    services.AddMassTransitHostedService();
    

    and your code will work.