Is it safe to inject a typed HTTP client inside an hosted service ? Does it create a captive dependency ? #24017
Unanswered
EnricoMassone
asked this question in
General
Replies: 1 comment
-
My question is very much in line with this other one dotnet/runtime#36067 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have a doubt related to the ASP.NET core dependency injection container. This question refers specifically to ASP.NET core 3.1.
I'm basically asking myself whether injecting a typed HTTP client inside an hosted service creates a so called captive dependency in terms of dependency injection.
The scenario I'm trying to depict is the following:
In the above code I'm injecting a transient dependency (
IStudentsApiClient
), inside a singleton (StudentsPollingHostedService
), thereby creating a captive dependency.Based on my understanding, this could totally harm the whole purpose of the ASP.NET core HTTP client factory implementation, which is aimed to return a new HTTP client each time one is requested by the consumer. In the code above we basically have an instance of HTTP client captured inside the hosted service for the whole application lifetime.
Do you agree that this is an actual issue ?
My idea to fix the above code is changing the
StudentsApiClient
so that theIHttpClientFactory
is used to create a new instance ofHttpClient
each timeGetAll
is called:Does this solve the issue of the first version of the code (if any) ?
Thanks for helping.
Beta Was this translation helpful? Give feedback.
All reactions