首页 > 解决方案 > Autofac 解析匹配LifetimeScope 集合

问题描述

我想从命名范围解析所有服务的 IEnumerable,但这不起作用,因为服务也从其他命名范围解析。

例子

var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<B>().As<IMyInterface>().InstancePerMatchingLifetimeScope("B");
containerBuilder.RegisterType<A>().As<IMyInterface>().InstancePerMatchingLifetimeScope("A");

IContainer container= containerBuilder.Build();

ILifetimeScope lifetimeScope = container.BeginLifetimeScope("A");
lifetimeScope.Resolve<IEnumerable<IMyInterface>>();

投掷 DependencyResolutionException: Unable to resolve the type 'ConsoleApp2.B' because the lifetime scope it belongs in can't be located.

有没有办法只解决当前范围内的服务?

我正在使用Autofac 6.0.0

标签: c#dependency-injectionautofac

解决方案


看来您遇到了错误。 有一个类似的问题已经解决了;我会将您的特定案例的重现添加到问题中,并将其更新为标记为错误而不是增强。

我目前不知道任何解决此问题的解决方法。对不起。


推荐阅读