我有一個程序,其中有一些帶有自定義接口的服務,但它們都繼承自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)...
1 回答

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
只需添加:
container.RegisterSingleton<IHttpServerService, HttpServerService>();
container.Collection.Register<IService>(
typeof(IHttpServerService),
// more implementations here
);
- 1 回答
- 0 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消