【发布时间】:2019-11-04 16:04:00
【问题描述】:
如何在 Autofac 模块中注册 Masstransit 消费者。
我有这个代码:
builder.AddMassTransit(configurator =>
{
configurator.AddConsumers(ThisAssembly);
//Bus
configurator.AddBus(c => c.Resolve<IBusFactory>().CreateBus());
});
在另一个模块中我有这个代码:
public class AutofacModule: Module
{
public override void Load(ContainerBuilder builder)
{
builder.RegisterConsumers(ThisAssembly);
}
}
但是 Masstransit 找不到位于 Modue 组件中的消费者。 请帮忙
编辑: 我有多个程序集(模块)没有被起始项目直接引用。程序集在应用程序启动时使用 /Modules 子文件夹中的 MEF 加载。消费者位于这些模块中。我使用 Autofac 与 MEF 的集成将 Autofac 模块加载到 Autofac 配置中。 当我说公共交通找不到消费者时,我的意思是: 当我放断点时吃掉了这条线
configurator.AddBus(...)
并检查 configurator._consumerRegistrations 字段,其中没有,只有来自启动应用程序的字段。此外,当我发布事件时,位于这些模块中的消费者都没有使用它。这些事件仅在启动应用程序中使用。
【问题讨论】:
-
您不需要像第二个代码 sn-p 中那样注册消费者,
.AddConsumers()方法会为您注册消费者。不清楚您所说的“大众运输未找到消费者”是什么意思,您能否使用您遇到的错误?此外,如果您打算使用参数,请不要使用_ => { },请为参数命名。这可能会导致问题。 -
我添加了更多细节并重命名了_。到配置器。
标签: c# module configuration autofac masstransit