C
C#2y ago
Head0nF1re

❔ HttpClient vs IHttpClientFactory

Hey. I have a few doubts regarding HttpClient, I was reading: - https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines - https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory#httpclient-lifetime-management And I was wondering why the IHttpClientFactory should only be used for short-lived connections (and what can be considered short-lived, like a few requests between a request flow?) ?
Also, in the context of a pulling service I assume that what I need is HttpClient without IHttpClientFactory (and configure PooledConnectionLifetime to prevent DNS problems) since it's requesting the same endpoint frequently, right? I'm thinking in having a singleton SomeServiceHttpClient for each endpoint being used in the pulling service. Is there a difference in having it static or as singleton, I kept seeing posts about people having DNS issues with it being singleton? Thanks
38 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
@TeBeConsulting Yes, I read that creating HttpClient frequently can lead to Port exhaustion and that's why it's recommended to use HttpClient as static or singleton with the PooledConnectionLifetime set to some value But the docs recommend to use HttpClient as static or singleton for long-lived connection
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
But, it isn't? The docs say otherwise
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
That's my doubt. Why can't IHttpClientFactory be used for long lived connection (according to docs recommendation)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
HttpClient guidelines for .NET - .NET
Learn about using HttpClient instances to send HTTP requests and how you can manage clients using IHttpClientFactory in your .NET apps.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
tebeco#0205
if you have the factory available, use it
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
@TeBeConsulting Sorry, I'm a bit confused. But, long lived also saves the connection at the kernel/tcp level right? Not only in the app. In the context of a "normal" app I can understand why it's better HttpClientFactory or HttpClient mapped to Scope. What about in the context of a pulling service that keeps running and only makes requests in order to pool?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Just assuming that because of the Port problem, since using static prevents that. Or is it unrelated, that Port problem with the connection being reused at kernel?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Okay, guess I will use Factory. But, how is it not long-lived since it's running forever making requests to the same url?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
every 5 seconds
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
mistyped, every 5 secs not 5 request in 5 secs
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Damn, so what can be considered long-lived lmao
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
So, either way (HttpClient vs HttpClientFactory), the tcp connection will be the same and reused. The difference in using one or the other is how dotnet handles the instance in the app?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
I'm probably the one that misundertood, I been working professionally for only 7/8 months and only started with C#/dotnet about 4 months ago kekw if I misunderstood, there's probably wrong in how docs are explaining it (at least from the perspective of someone new)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Well, guess it's fine then 😆 . This is just pulling from on service and sending a request to another. What type of client do you normally use (named, typed)?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Are you saying that's the rare case where you would use a named one? Assuming I do that, that's because I wanted 2 types of HttpClient based on one "base" httpclient, no? If it was named I would need 2 named clients instead of 1 Generic
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Think you need to map to the concrete implementation right? AddHttpClient<IOne, A> I dont know, but I'm reading typed client are Transient so there's probably a use case? 🤷‍♂️ in here: https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory#use-typed-clients-in-singleton-services
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Using typed clients in singleton services can be dangerous. For more information, see the Using Typed clients in singleton services section.
Typed clients are expected to be short-lived in the same sense as HttpClient instances created by IHttpClientFactory (for more information, see HttpClient lifetime management). As soon as a typed client instance is created, IHttpClientFactory has no control over it. If a typed client instance is captured in a singleton, it may prevent it from reacting to DNS changes, defeating one of the purposes of IHttpClientFactory.
If you need to use HttpClient instances in a singleton service, consider the following options: - Use the named client approach instead, injecting IHttpClientFactory in the singleton service and recreating HttpClient instances when necessary. - If you require the typed client approach, use SocketsHttpHandler with configured PooledConnectionLifetime as a primary handler. For more information on using SocketsHttpHandler with IHttpClientFactory, see the section Using IHttpClientFactory together with SocketsHttpHandler.
> So, typed client can have the same problem with the DNS thing that is solved by PooledConnectionLifetime
MODiX
MODiX2y ago
tebeco#0205
and don't use singletong when you don't need singleton
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Head0nF1re
Head0nF1reOP2y ago
Sorry, this thread has become quite long 😅
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server