I am having some issues with MediatR using AutoFac. My registration code is below which I have lifted and slightly modified from GitHub. The class EntityContext
is within the main code assembly, and Assembly
is set to that 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();
None of my IRequestHandler registrations are available for resolution using
using (var scope = container.BeginLifetimeScope())
{
scope.Resolve<AddTemplateHandler>();
}
An Explicit registration of the types above also does not work. Can anyone help?