[英]Resolve all registered services to a single interface from ASP.NET Core IOC container
The ASP.NET Core built-in dependency injection mechanism allows to have multiple service registrations to the same interface type: ASP.NET 内核内置依赖注入机制允许对同一接口类型进行多个服务注册:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<ICustomService, CustomService1>();
services.AddScoped<ICustomService, CustomService2>();
services.AddScoped<ICustomService, CustomService3>();
...
}
While the last service registered get a precedence when the requested service is resolved:当请求的服务被解析时,最后注册的服务获得优先权:
public MyController(ICustomService myService) { }
I'm wandering how can i get the full list of registered services of a given type in my controller constructor eg.我在徘徊如何在我的 controller 构造函数中获取给定类型的注册服务的完整列表,例如。 ICustomService?
自定义服务?
Make constructor argument a collection使构造函数参数成为一个集合
public class MyController
{
public MyController(IEnumerable<ICustomService> myServices) { }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.