Reputation: 214
HTTP Client Factory in aspnet core
Upvotes: 0
Views: 2926
Reputation: 155418
WARNING: This answer concerns the state of HttpClient
and IHttpClientFactory
as of .NET Core 2.1 in 2018. It's now October 2021 and there have been a lot of changes to .NET since then (especially in .NET 5 and now .NET 6) so this answer may be obsolete and incorrect if you're reading this SO post in long since I originally researched and wrote this back in 2018.
I found this article which answers your question (emphasis mine):
https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore
The expensive part of using
HttpClient
is actually creating theHttpClientHandler
and the connection. Having these pooled in this manner means we can get more efficient use of the connections on our system. When you use theHttpClientFactory
to request aHttpClient
, you do in fact get a new instance each time, which means we don’t have to worry about mutating its state. ThisHttpClient
may (or may not) use an existingHttpClientHandler
from the pool and therefore use an existing open connection.
Upvotes: 2
Reputation: 899
Upvotes: 0