C# 如何通过Autofac模块配置Masstransit Consummers
如何在Autofac模块中注册Masstransit用户 我有以下代码:C# 如何通过Autofac模块配置Masstransit Consummers,c#,module,configuration,autofac,masstransit,C#,Module,Configuration,Autofac,Masstransit,如何在Autofac模块中注册Masstransit用户 我有以下代码: builder.AddMassTransit(configurator => { configurator.AddConsumers(ThisAssembly); //Bus configurator.AddBus(c => c.Resolve<IBusFactory>().CreateBus());
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 assembly中的使用者。
请帮忙
编辑:
我有多个启动项目未直接引用的程序集(模块)。程序集在应用程序启动时使用MEF从/Modules子文件夹加载。耗电元件位于这些模块中。我使用Autofac与MEF集成,将Autofac模块加载到Autofac配置中。
当我说公共交通找不到消费者时,我的意思是:
当我把一个断点吃线
configurator.AddBus(…)
并检查configurator.\u consumerRegistrations字段,其中没有任何内容,只有启动应用程序中的内容。此外,当我发布事件时,这些模块中的使用者都没有使用它。事件仅在启动的应用程序中使用。加载Autofac模块并在容器中注册所有使用者后,您可以使用以下方法注册使用者(和SAGA)
public static void AddConsumersFromContainer(此IRegistrationConfigurator配置程序,IComponentContext上下文)
{
var consumerTypes=context.FindTypes(isconsumerdefinition);
configurator.AddConsumers(消费者类型);
}
公共静态void AddSagasFromContainer(此IRegistrationConfigurator配置程序,IComponentContext上下文)
{
var sagaTypes=context.FindTypes(IsSagaOrDefinition);
configurator.AddSagas(sagaTypes);
}
静态类型[]FindTypes(此IComponentContext上下文,Func筛选器)
{
返回context.ComponentRegistry.Registrations
.SelectMany(r=>r.Services.OfType(),(r,s)=>(r,s))
.其中(rs=>过滤器(rs.s.ServiceType))
.选择(rs=>rs.s.ServiceType)
.ToArray();
}
///
///如果类型是使用者或使用者定义,则返回true
///
///
///
公共静态bool isconsumerdefinition(类型)
{
Type[]interfaces=Type.GetTypeInfo().GetInterfaces();
返回接口.Any(t=>t.HasInterface(typeof(IConsumer))| | t.HasInterface(typeof(IConsumerDefinition));
}
///
///如果类型是saga或saga定义,则返回true
///
///
///
公共静态布尔ISSAGAORD定义(类型)
{
Type[]interfaces=Type.GetTypeInfo().GetInterfaces();
if(interfaces.Contains(typeof(ISaga)))
返回true;
返回接口.Any(t=>t.HasInterface(typeof(initialedby))
||t.HasInterface(类型(编排))
||t.HasInterface(类型(观察者))
||t.HasInterface(类型(IsagadDefinition));
}
加载Autofac模块并在容器中注册所有使用者后,您可以使用以下步骤注册使用者(和sagas)
public static void AddConsumersFromContainer(此IRegistrationConfigurator配置程序,IComponentContext上下文)
{
var consumerTypes=context.FindTypes(isconsumerdefinition);
configurator.AddConsumers(消费者类型);
}
公共静态void AddSagasFromContainer(此IRegistrationConfigurator配置程序,IComponentContext上下文)
{
var sagaTypes=context.FindTypes(IsSagaOrDefinition);
configurator.AddSagas(sagaTypes);
}
静态类型[]FindTypes(此IComponentContext上下文,Func筛选器)
{
返回context.ComponentRegistry.Registrations
.SelectMany(r=>r.Services.OfType(),(r,s)=>(r,s))
.其中(rs=>过滤器(rs.s.ServiceType))
.选择(rs=>rs.s.ServiceType)
.ToArray();
}
///
///如果类型是使用者或使用者定义,则返回true
///
///
///
公共静态bool isconsumerdefinition(类型)
{
Type[]interfaces=Type.GetTypeInfo().GetInterfaces();
返回接口.Any(t=>t.HasInterface(typeof(IConsumer))| | t.HasInterface(typeof(IConsumerDefinition));
}
///
///如果类型是saga或saga定义,则返回true
///
///
///
公共静态布尔ISSAGAORD定义(类型)
{
Type[]interfaces=Type.GetTypeInfo().GetInterfaces();
if(interfaces.Contains(typeof(ISaga)))
返回true;
返回接口.Any(t=>t.HasInterface(typeof(initialedby))
||t.HasInterface(类型(编排))
||t.HasInterface(类型(观察者))
||t.HasInterface(类型(IsagadDefinition));
}
您不需要像在第二个代码片段中那样注册使用者,.AddConsumers()
方法为您注册它们。不清楚你所说的“大众运输公司找不到消费者”是什么意思,你能说出你得到的错误吗?另外,不要使用\u=>{}
如果您计划使用该参数,请为该参数指定一个名称。这可能会导致问题。我已添加了更多详细信息,并重命名了389;。您不需要像在第二个代码段中那样注册使用者,.AddConsumers()
方法为您注册它们。不清楚你所说的“大众运输公司找不到消费者”是什么意思,你能说出你得到的错误吗?另外,不要使用\u=>{}
如果您计划使用该参数,请为该参数指定一个名称。这可能会导致问题。我已添加了更多详细信息,并重命名了389;。我在以下文件中使用它:builder.AddMassTransit(configurator=>{configurator.AddConsumersFromContainer();//总线配置器.AddBus(c=>c.Resolve().CreateBus());});但是我从哪里获得ComponentContext呢?它是.AddBus()中的c
public static void AddConsumersFromContainer(this IRegistrationConfigurator configurator, IComponentContext context)
{
var consumerTypes = context.FindTypes(IsConsumerOrDefinition);
configurator.AddConsumers(consumerTypes);
}
public static void AddSagasFromContainer(this IRegistrationConfigurator configurator, IComponentContext context)
{
var sagaTypes = context.FindTypes(IsSagaOrDefinition);
configurator.AddSagas(sagaTypes);
}
static Type[] FindTypes(this IComponentContext context, Func<Type, bool> filter)
{
return context.ComponentRegistry.Registrations
.SelectMany(r => r.Services.OfType<IServiceWithType>(), (r, s) => (r, s))
.Where(rs => filter(rs.s.ServiceType))
.Select(rs => rs.s.ServiceType)
.ToArray();
}
/// <summary>
/// Returns true if the type is a consumer, or a consumer definition
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsConsumerOrDefinition(Type type)
{
Type[] interfaces = type.GetTypeInfo().GetInterfaces();
return interfaces.Any(t => t.HasInterface(typeof(IConsumer<>)) || t.HasInterface(typeof(IConsumerDefinition<>)));
}
/// <summary>
/// Returns true if the type is a saga, or a saga definition
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsSagaOrDefinition(Type type)
{
Type[] interfaces = type.GetTypeInfo().GetInterfaces();
if (interfaces.Contains(typeof(ISaga)))
return true;
return interfaces.Any(t => t.HasInterface(typeof(InitiatedBy<>))
|| t.HasInterface(typeof(Orchestrates<>))
|| t.HasInterface(typeof(Observes<,>))
|| t.HasInterface(typeof(ISagaDefinition<>)));
}