0

Below is my Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpClient();
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

And controller:

[ApiController]
    [Route("[controller]")]
    public class TestController : ControllerBase
    {
        private readonly HttpClient _httpClient;
        private readonly IHttpClientFactory _httpClientFactory;
        public TestController(HttpClient httpClient, IHttpClientFactory httpClientFactory)
        {
            _httpClient = httpClient;
            _httpClientFactory = httpClientFactory;
        }

        [HttpGet]
        [Route("getdata")]
        public async Task GetData()
        {
            var baseAddress = "http://youtube.com";

            var response = await _httpClient.GetAsync(baseAddress);

            var client = _httpClientFactory.CreateClient();
            response = await client.GetAsync(baseAddress);
        }
    }

As you can see I can get an instance of HttpClient in two ways:

  1. By Injecting HttpClient
  2. By Injecting IHttpClientFactory and then _httpClientFactory.CreateClient();

Though I am getting responses using both instances, my question is what is the difference between them? And when to use which one?

Yeasin Abedin
  • 2,081
  • 4
  • 23
  • 41
  • 1
    Does this answer your question? [HttpClientFactory.Create vs new HttpClient](https://stackoverflow.com/questions/18976042/httpclientfactory-create-vs-new-httpclient) – Mohammad Aghazadeh Mar 14 '22 at 11:05

1 Answers1

0

If you just post signal request.there's even no difference between injecting an instance of httpclient and creating a instance of httpclient with httpclientfactory .

If you use several instances of httpclient or reuse httpclient to post multiple requests, some problems may occur, using httpclient could handle these problems.

You could create httpclients with different settings as follows:

In startup.cs:

services.AddHttpClient("client_1", config =>  
            {
                config.BaseAddress = new Uri("http://client_1.com");
                config.DefaultRequestHeaders.Add("header_1", "header_1");
            });
services.AddHttpClient("client_2", config =>
            {
                config.BaseAddress = new Uri("http://client_2.com");
                config.DefaultRequestHeaders.Add("header_2", "header_2");
            }); 

In your controller:

var client1 = _httpClientFactory.CreateClient("client_1");
var client2 = _httpClientFactory.CreateClient("client_2");
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11