如何在 DryIoc Mvc 控制器中注册 IEnumerable<IService>,如 Autofac Enumeration (IEnumerable<B>, IList<B>, ICollection<B>)
How to register IEnumerable<IService> In DryIoc Mvc Controller like Autofac Enumeration (IEnumerable<B>, IList<B>, ICollection<B>)
使用 Autofac 测试代码正常,但使用 DryIoc 时出错。如何使这项工作。
public class HomeController : Controller
{
private readonly ITestAppService _testAppService;
private readonly IEnumerable<ITestAppService> _testAppServiceList;
public HomeController(ITestAppService testAppService, IEnumerable<ITestAppService> testAppServiceList)
{
_testAppService = testAppService;
_testAppServiceList = testAppServiceList;
}
}
public class Test1AppService : ITestAppService{}
public class Test2AppService : ITestAppService{}
public interface ITestAppService: IAppService{}
错误 'Make sure that the controller has a parameterless public constructor ' 是由控制器中的 ITestAppService 字段引起的,而不是字段 IEnumerable.Following 是我的注册码。
var impls =
typeof(IAppService).Assembly
.GetTypes()
.Where(type =>
type.IsPublic &&
!type.IsAbstract &&
type.GetInterfaces().Length != 0 && typeof(IAppService).IsAssignableFrom(type));
foreach (var type in impls)
{
container.Register(type.GetInterfaces().First(), type, Reuse.Transient);
}
根据dadhi的建议解决,我的容器是
DryIoc.IContainer container = new DryIoc.Container(
rules =>
{
return rules.WithFactorySelector(Rules.SelectLastRegisteredFactory())
.WithResolveIEnumerableAsLazyEnumerable();
}
);
关于从多个默认服务中解析 的 dryioc wiki 可能需要一些更新。 Rules.SelectLastRegisteredFactory是一种方法。
问题是考虑 ITestAppService
容器 doesn't know which to select for the first dependency 的两个实现。但是您可以明确定义约定:
var container = new Container(rules => rules
.WithFactorySelector(Rules.SelectLastRegisteredFactory));
使用 Autofac 测试代码正常,但使用 DryIoc 时出错。如何使这项工作。
public class HomeController : Controller
{
private readonly ITestAppService _testAppService;
private readonly IEnumerable<ITestAppService> _testAppServiceList;
public HomeController(ITestAppService testAppService, IEnumerable<ITestAppService> testAppServiceList)
{
_testAppService = testAppService;
_testAppServiceList = testAppServiceList;
}
}
public class Test1AppService : ITestAppService{}
public class Test2AppService : ITestAppService{}
public interface ITestAppService: IAppService{}
错误 'Make sure that the controller has a parameterless public constructor ' 是由控制器中的 ITestAppService 字段引起的,而不是字段 IEnumerable.Following 是我的注册码。
var impls =
typeof(IAppService).Assembly
.GetTypes()
.Where(type =>
type.IsPublic &&
!type.IsAbstract &&
type.GetInterfaces().Length != 0 && typeof(IAppService).IsAssignableFrom(type));
foreach (var type in impls)
{
container.Register(type.GetInterfaces().First(), type, Reuse.Transient);
}
根据dadhi的建议解决,我的容器是
DryIoc.IContainer container = new DryIoc.Container(
rules =>
{
return rules.WithFactorySelector(Rules.SelectLastRegisteredFactory())
.WithResolveIEnumerableAsLazyEnumerable();
}
);
关于从多个默认服务中解析 的 dryioc wiki 可能需要一些更新。 Rules.SelectLastRegisteredFactory是一种方法。
问题是考虑 ITestAppService
容器 doesn't know which to select for the first dependency 的两个实现。但是您可以明确定义约定:
var container = new Container(rules => rules
.WithFactorySelector(Rules.SelectLastRegisteredFactory));