作为 MassTransit 和 RabbitMQ 的新用户,我目前正在尝试让我的 ASP.NET 核心服务与 MassTransit 一起工作。
拿这个documentation配置 MassTransit 和 ASP.NET Core 的部分我无法让它工作。
目前(部分)Startup.cs 看起来像
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 中自动创建的,但没有队列绑定(bind)到我期望的交换。
调用我的 API 端点后,我可以看到交换器上的事件,但当然消费者什么也不做,因为没有队列。
我遗漏了什么(明显的)部分?
最佳答案
好的,我发现了问题。在编写文档时,它按照文档中的描述工作。有几个AddMassTransit
IServiceCollection
的扩展名界面,令人困惑。
AddMassTransit
接受总线实例的重载按照描述的方式工作。
AddMassTransit
重载,它接受 Action<IServiceCollectionConfigurator>
只做必要的注册。
你需要添加一行:
services.AddMassTransitHostedService();
您的代码将起作用。
关于c# - ASP.NET Core 服务不创建 RabbitMQ 队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56791857/