HttpClient DI or USING()
Which one is the best, Dependency injection or instance by using
21 Replies
I will use this althrough my service
will you be using clients a lot throughout your app?
specific in one service
Avoid using
i'd just have one static client then
Keep the client around for a long time
There are tradeoffs to be aware of.keeping a client around a long time can lead to issues with dns
Clients cache dns resolutions and dont refresh the cache
But disposing the http handler frequently leads to resource exhaustion
Request wont be frequently
The solution is the Microsoft.Extensions.HttpClientFactory nuget
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
If its infrequent, as in less than once every minute or two than its fine
you mean its fine DI ?
Yeah, use di
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
If its frequent, register as singleton, otherwise transient
HttpClientFactory is nice because it makes it easy to set up specific http clients for each service you need
I need to do dispose or something specific ?
Not in di
Di handles the dispose
awesome
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
thanks a lot
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
already did by this way?