我使用AutoFac与MediatR有一些问题。我的注册码低于我从GitHub取消并略微修改。类EntityContext
位于主代码程序集中,Assembly
设置为该程序集:
var builder = new Autofac.ContainerBuilder();
builder.RegisterApiControllers(Assembly);
if (config != null)
{
builder.RegisterWebApiFilterProvider(config);
}
builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterAssemblyTypes(typeof(EntityContextFactory).GetTypeInfo().Assembly).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(Ping).GetTypeInfo().Assembly).Where(t =>
t.GetInterfaces().Any(i => TypeExtensions.IsClosedTypeOf(i, typeof(IRequestHandler<,>))
|| TypeExtensions.IsClosedTypeOf(i, typeof(IAsyncRequestHandler<,>))
||
TypeExtensions.IsClosedTypeOf(i,
typeof(ICancellableAsyncRequestHandler<,>))
|| TypeExtensions.IsClosedTypeOf(i, typeof(INotificationHandler<>))
|| TypeExtensions.IsClosedTypeOf(i, typeof(IAsyncNotificationHandler<>))
||
TypeExtensions.IsClosedTypeOf(i,
typeof(ICancellableAsyncNotificationHandler<>))
)
)
.AsImplementedInterfaces();
builder.RegisterInstance(Console.Out).As<TextWriter>();
builder.Register<SingleInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t =>
{
object o;
return c.TryResolve(t, out o) ? o : null;
};
});
builder.Register<MultiInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
});
builder.RegisterConsumers(Assembly);
Container = builder.Build();
我的IRequestHandler注册均无法使用
进行解析using (var scope = container.BeginLifetimeScope())
{
scope.Resolve<AddTemplateHandler>();
}
上述类型的显式注册也不起作用。 有人可以帮忙吗?