简体   繁体   中英

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:

    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. ICustomService?

Make constructor argument a collection

public class MyController
{
    public MyController(IEnumerable<ICustomService> myServices) { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM