❔ 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
Also, in the context of a pulling service I assume that what I need is
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?
Thanks38 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
@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
connectionUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
But, it isn't? The docs say otherwise
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
That's my doubt. Why can't IHttpClientFactory be used for long lived connection (according to docs recommendation)
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
tebeco#0205
if you have the factory available, use it
Quoted by
<@!689473681302224947> from #HttpClient vs IHttpClientFactory (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
@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•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
every 5 seconds
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
mistyped, every 5 secs not 5 request in 5 secs
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Damn, so what can be considered long-lived lmao
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
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 if I misunderstood, there's probably wrong in how docs are explaining it (at least from the perspective of someone new)
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
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-servicesUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
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
tebeco#0205
and don't use singletong when you don't need singleton
Quoted by
<@!689473681302224947> from #HttpClient vs IHttpClientFactory (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Sorry, this thread has become quite long 😅
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.