0

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?

KnowHoper
  • 4,352
  • 3
  • 39
  • 54
  • why are YOU trying to resolve the AddTemplateHandler - that's the job of the mediator – Alex Jun 08 '17 at 09:35
  • It's an integration test. I guess I could just return a concrete call and resolve the constructor dependencies via the container. Will try this. – KnowHoper Jun 08 '17 at 09:44

0 Answers0