C
C#2y ago
Cracker

❔ Ctor DI vs IServiceProvider.GetService

How often and when do you use IServiceProvider.GetService() for retrieving dependency over injecting dependency through constructor ?
5 Replies
Cracker
CrackerOP2y ago
I have faced an issue where there was a dependency I was injecting through ctor and that dependency service was population some data on instantiation, one of my API was lack of that config and was getting error. Therefore I switch that to IServiceProvider.GetService() and resolve the dependency only if required config is exists. That was workaround for me but it's likely the first time I am using IServiceProvider myself, I have seen same usecases but still..
Monsieur Wholesome
One doesnt have to do with the other Or rather, the thing you need the dependency for would be coming within the IServiceProvider as well, where the provider manages the DI for ya
Cracker
CrackerOP2y ago
I thought so, both way uses the same DI container to resolve specified dependency
phaseshift
phaseshift2y ago
I only ever use it indirectly via activatorutilities used in factories
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server