C
C#2y ago
Vinicius

HttpClient DI or USING()

Which one is the best, Dependency injection or instance by using
21 Replies
Vinicius
Vinicius2y ago
I will use this althrough my service
ero
ero2y ago
will you be using clients a lot throughout your app?
Vinicius
Vinicius2y ago
specific in one service
Cisien
Cisien2y ago
Avoid using
ero
ero2y ago
i'd just have one static client then
Cisien
Cisien2y ago
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
Vinicius
Vinicius2y ago
Request wont be frequently
Cisien
Cisien2y ago
The solution is the Microsoft.Extensions.HttpClientFactory nuget
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Cisien
Cisien2y ago
If its infrequent, as in less than once every minute or two than its fine
Vinicius
Vinicius2y ago
you mean its fine DI ?
Cisien
Cisien2y ago
Yeah, use di
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Cisien
Cisien2y ago
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
Vinicius
Vinicius2y ago
I need to do dispose or something specific ?
Cisien
Cisien2y ago
Not in di Di handles the dispose
Vinicius
Vinicius2y ago
awesome
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Vinicius
Vinicius2y ago
thanks a lot
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Vinicius
Vinicius2y ago
already did by this way?
Want results from more Discord servers?
Add your server
More Posts