0

我在使用 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>();
            }

上述类型的显式注册也不起作用。任何人都可以帮忙吗?

4

0 回答 0