kav
kav

Reputation: 21

What is the purpose of the HttpClientFactory (System.Net.Http.Formatting)?

There is a lot of talk on the Internet about HttpClient, IHttpClientFactory, DI, socket exhaustion problem, etc.

Can someone explain to me why the HttpClientFactory class (from System.Net.Http.Formatting assembly) is needed and its methods.

Upvotes: 1

Views: 1747

Answers (1)

poke
poke

Reputation: 387537

System.Net.Http.HttpClientFactory, which is shipped with the Microsoft.AspNet.WebApi.Client NuGet package, is just a helper to create a HttpMessageHandler with a pipeline of multiple HttpDelegatingHandler objects. Its Create method literally does this:

public static HttpClient Create(HttpMessageHandler innerHandler, params DelegatingHandler[] handlers)
{
    HttpMessageHandler pipeline = CreatePipeline(innerHandler, handlers);
    return new HttpClient(pipeline);
}

And the CreatePipeline sets up a single HttpMessageHandler that wrapps all the passed handlers into a single pipeline, so that they get executed in order.

This HttpClientFactory has little to do with the newer IHttpClientFactory that’s part of the Microsoft.Extensions.Http package. That one is what all the “buzz” is about and how you should ideally create HTTP clients in the future.

Upvotes: 4

Related Questions