Inject http client only for specified service [Answered]
I have a RemoteApiClient that I want to inject to the FooBarService and only to the FooBarService. The reason for this is that the RemoteApiClient is not really meant to be used directly. How do I achieve this? Additionally, the IMemoryCache is injected into the FooBarService, but the IMemoryCache is also used elsewhere.
9 Replies
Have FooBarService as a param in your RemoteApiClient constructor? or am I missing something
The RemoteApiClient is a dependency of FooBarService. FooBarService must use the RemoteApiClient internally.
you can't restrict which services your registered dependencies can be injected to, outside of not requesting them in the constructors of other services
kind of goes against the spirit of DI
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Alright thank you guys for the answers. I’ll see if I’ll go the way with the second service collection or if I’ll just not bother changing it.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
It's definitely not what I want 😄
two service collections sounds like an antipattern
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
✅ This post has been marked as answered!