ASP.NET Core服务未创建RabbitMQ队列 [英] ASP.NET Core service not creating RabbitMQ queue

查看:411
本文介绍了ASP.NET Core服务未创建RabbitMQ队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为MassTransit和RabbitMQ的新用户,我目前正在尝试使我的ASP.NET核心服务与MassTransit一起使用.

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

采用此文档部分进行配置MassTransit和ASP.NET Core我无法使其正常运行.

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

当前(部分)Startup.cs类似于

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

交换是在启动时在RabbitMQ中自动创建的,但是没有队列绑定到我期望的交换.

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

调用API端点后,我可以看到交换上的活动,但是由于没有队列,因此消费者当然不执行任何操作.

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?

推荐答案

好,我发现了问题.撰写文档时,其工作方式与文档中所述的相同. IServiceCollection接口有几个AddMassTransit扩展名,这令人困惑.

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重载,它接受总线实例的工作按所述.

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

AddMassTransit重载,仅接受必要的注册.

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

您需要添加一行:

services.AddMassTransitHostedService();

您的代码将可用.

这篇关于ASP.NET Core服务未创建RabbitMQ队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆