C
C#3y ago
Vinicius

HttpClient DI or USING()

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