Back to Practical Aspnetcore

IServiceProvider

projects/dependency-injection/dependency-injection-2/README.md

latest536 B
Original Source

IServiceProvider

IServiceProvider is a service locator. It allows you to obtain objects from the dependency injection system. It is considered a bad practice in common scenario.

It is better to be explicit of your dependency in your constructor e.g. MyObject(ILogger<MyObject> logger, IStringLocalizer<MyObject>) than using MyObject(IServiceProvider provider) and obtain those objects via the provider.

However when you encounter a situation where using a service locator is needed, IServiceProvider is there for you.