【发布时间】:2018-08-09 09:21:44
【问题描述】:
我有一个程序,其中有一些带有自定义接口的服务,但它们都继承自 IService。这些服务在 Simple Injector 容器中使用它们的自定义接口注册为单例,但在一个主类中,我希望它们都使用IEnumerable<IService> 注入到构造函数中。这不起作用,因为IEnumerable<IService> 未注册。我的问题是如何配置容器以允许在构造函数中检索它们——我可以手动解析类型,但我应该避免这种反模式。
// sample service (one of many)
public interface IHttpServerService : IService
...
_container.RegisterSingleton<IHttpServerService, HttpServerService>();
...
public Controller(IEnumerable<IService> services)
...
【问题讨论】:
-
你能给我们看一些代码吗?
-
我添加了一些伪代码——idead是基本的所以没有太多代码
标签: c# dependency-injection simple-injector