ASP.NET Core service not creating RabbitMQ queue

一曲冷凌霜 提交于 2019-12-08 21:48:01

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/56791857/asp-net-core-service-not-creating-rabbitmq-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!