Siamak
Siamak

Reputation: 1

How to get the list of injected services to a controller

I need to get the list of all injected services of a specific type to a controller in an ASP.NET MVC 5 application. I am using Microsoft.Extensions.DependencyInjection as dependency injection container. I tried GetServices but it does not work.

serviceProvider.GetServices(typeof(IGenericRepository<,,>)).ToList()

serviceProvider is one of the services injected to the controller (IServiceProvider). As a test case, I can get one service using the following line of code (below), but I can't get the list of services.

IGenericRepository<CASES_BY_SUBTYPE, int, int> _CaseSubType = _serviceProvider.GetService<IGenericRepository<CASES_BY_SUBTYPE, int, int>>();

Upvotes: 0

Views: 2575

Answers (1)

Brando Zhang
Brando Zhang

Reputation: 27997

As far as I know, there is no build-in method could get all the service by using service provider, the GetServices method is just return a single service. If you still want to get all the services, you should build it by yourself.

I suggest you could refer to this answer to know how to build it. The replier get the codes from asp.net core unit test source codes.

Upvotes: 1

Related Questions