简体   繁体   English

将所有注册的服务从 ASP.NET Core IOC 容器解析到单个接口

[英]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.

相关问题 ASP.NET Core 3.x - 访问 IoC 容器 - ASP.NET Core 3.x - Access IoC Container ASP.NET Core与现有的IoC容器和环境? - ASP.NET Core with existing IoC container & environment? ASP.Net Core从DI容器获取服务 - ASP.Net Core Get Services From DI Container 如何从Asp.net核心依赖注入中注册的子类解析父类实例 - How to resolve parent class instance from registered child class in Asp.net core dependency injection 有没有一种方法可以解决所有在Windsor IoC容器中注册的组件? - Is there a way to eagerly resolve all components registered with Windsor IoC container? 从MVVM Cross IoC容器访问注册的服务 - Accessing registered services from MVVM Cross IoC Container 获取 ASP.NET Core 中所有注册的路由 - Get all registered routes in ASP.NET Core 在 Asp.Net Core 中实现另一个接口来解析 DBContext - Resolve DBContext with implements another interface in Asp.Net Core Autofac 作为 AWS 中的 IoC 容器 Lambda 无服务器 ASP.NET Core 3.1 Web ZDB974238714CA8DE64FZACE7 - Autofac as IoC container in AWS Lambda Serverless ASP.NET Core 3.1 Web API 使用 ASP.NET Core 内置 IoC 容器注入多个客户端而不是一个 - Injecting multiple clients instead of just one using ASP.NET Core built-in IoC container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM