question

HanaaGebril-8901 avatar image
0 Votes"
HanaaGebril-8901 asked kashyapa answered

IServiceCollection Access

How can I access IServiceCollection from web api or controller?

dotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@HanaaGebril-8901,
Why do you want to get IServiceCollection in controller?If you want to access and replace IServiceCollection in controller,you cannot do that,because it's impossible to change the configuration of the Container once the application is started.

1 Vote 1 ·

1 Answer

kashyapa avatar image
0 Votes"
kashyapa answered

@HanaaGebril-8901

Not sure what you are trying to achieve. You will need to provide more explanation on your question.

The concept of IServiceCollection is - its a container which is built up at the start of your app. You register components with the collection with lifetime specified (Singleton, Scoped etc). Once your app starts and container is built with all your dependent components, you just ask for the component you need through your constructor dependency. ASP.NET Core makes sure that whatever dependency a class is asking for will be picked up from your container and provided to the class. Thats DI & IoC principle in play...

So when you say i want to access IServiceCollection - what for ?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.