4

There is a similar SO question here which talks about the performance of HttpClient objects and recommends to use one HttpClient instance per application. My project requires me to send multiple webservice requests to the same URI but each with a different set of headers. Should I create a new HttpClient instance for every request, knowing the fact that DefaultRequestHeaders will be same for all requests if I use a static instance.

1
  • Although you shouldn't create a new HttpClient for each instance, it's not a problem to create a few different ones for different purposes. Commented Sep 23, 2018 at 17:13

1 Answer 1

12

You can:

  • Set default headers on your global instance
  • Create multiple global (logical) instances with different default configurations
  • Set (additional) headers per request
  • Create new HttpClient using IHttpClientFactory

Docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1

Global Default Headers

Here you create one client instance and add headers that will be applied to all requests.

 var client = new HttpClient();
 client.DefaultRequestHeaders.Add("Content-Type", contentTypeValue);

Multiple Preconfigured Instances

In this dotnet core 2.1 example, we register a preconfigured named instance:

services.AddHttpClient("github", c =>
{
    c.BaseAddress = new Uri("https://api.github.com/");
    // Github API versioning
    c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
    // Github requires a user-agent
    c.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample");
});

Later we'll inject IHttpClientFactory, and get this pre configured client like so:

var client = _clientFactory.CreateClient("github");

An alternative to named client are "typed clients": https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0#typed-clients. Both approaches will keep the basic configuration at one place.

Headers per Request

If your headers belong to a single request only, simple set them per request.

var client = new HttpClient();

var request = new HttpRequestMessage();
request.Headers.Add("Content-Type", "text/plain");

var response = await client.SendAsync(request);

Using this approach you can use a shared HttpClient instance.

IHttpClientFactory

If you want a new "clean" HttpClient instance, the recommended approach for asp.net core is to inject IHttpClientFactory and use _clientFactory.CreateClient().

public class MyService {
  public MyService (IHttpClientFactory clientFactory)
  {
    _clientFactory = clientFactory;
  }
  public async Task DoSomething()
  {
    var client = _clientFactory.CreateClient();
    // do request
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am implementing my logic as per "Headers per Request" approach in your answer. I will let you guys know about the performance implications and accept the answer if it works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.