ASP.NET Core service not creating RabbitMQ queue
Asked Answered
M

2

7

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?

Michale answered 27/6, 2019 at 13:14 Comment(1)
Yeah, the MT hosted service doesn't get registered. Can you put your repo on GitHub? I can have a quick look.Nocturnal
N
10

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.

Nocturnal answered 27/6, 2019 at 15:51 Comment(3)
Thank you! This helps a lot :-) Although now I'm getting MassTransit bus is not ready in the health checks. Queues and exchanges are created though!Michale
Fixed the MassTransit bus is not ready by adding : cfg.UseHealthCheck(provider);Michale
Yeah, I believe that was in the docs :) Still need to clarify this, thanks for pointing it out :)Nocturnal
B
0

According to the latest MT version, services.AddMassTransitHostedService(); was deprecated.

This is related to the RabbitMQ.

Let's say you have a program which publish the messages to the queue and configured the exchanges for them and you have another program to consume messages from the queue.

If you run only the publisher program, it will not create a defined queue. When you run the consumer program, it will check the queue existence and if it is not there it will create a queue and bind with the defined exchanges. As a advice, Make sure to run the consumer program before publishing any messages to the queue. Otherwise you can see RabbitMQ UI that showing message has arrived to the exchange but not in the queue.

I am sharing my thoughts regarding on this since I currently have been working on the .NET core web API project which publish the messages to the queue and have another worker project to consume messages from the queue.

I think this will help you to resolve your matter. Thanks.

Bonham answered 3/5, 2024 at 5:39 Comment(1)
Do you have an alternative to services.AddMassTransitHostedService() ?Eel

© 2022 - 2025 — McMap. All rights reserved.