Tech
Tech

Reputation: 214

Does the HTTPCientFactory in asp.net core keep the connection alive even after the request is completed like HttpClient in .net Framework

HTTP Client Factory in aspnet core

  1. Is this similar to having HTTPClient Pools ?
  2. Does the client inside the client factory keeps the connecting alive even after the request is completed
  3. Is the client inside the HttpClientFactory reused or does the HttpClientFactory creates new client for every request?

Upvotes: 0

Views: 2926

Answers (2)

Dai
Dai

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 the HttpClientHandler 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 the HttpClientFactory to request a HttpClient, you do in fact get a new instance each time, which means we don’t have to worry about mutating its state. This HttpClient may (or may not) use an existing HttpClientHandler from the pool and therefore use an existing open connection.

Upvotes: 2

J S
J S

Reputation: 899

  1. It has its own handlers and pools.
  2. No, it's returned to the pool, and free.
  3. The client is reused but in different pools.

Upvotes: 0

Related Questions