Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Resolving different HttpClient

Given the following classes:

public class HttpHelper
{
    private readonly HttpClient client;
    public HttpHelper(HttpClient client)
    {
        this.client = client;
    }
}

public class ServiceA
{
    private readonly HttpHelper helper;
    public ServiceA(HttpHelper helper)
    {
        this.helper = helper;
    }
 }

 public class ServiceB
 {
    private readonly HttpHelper helper;
    public ServiceB(HttpHelper helper)
    {
        this.helper = helper;
    }
}

and the following setup:

      sc.AddSingleton<ServiceA>()
         .AddHttpClient<HttpHelper>()
         .ConfigureHttpClient((sp, client) => { client.BaseAddress = new Uri("http://domainA"); });

      sc.AddSingleton<ServiceB>()
        .AddHttpClient<HttpHelper>()
        .ConfigureHttpClient((sp, client) => { client.BaseAddress = new Uri("http://domainB"); });

When I try to resolve ServiceA and ServiceB they both get an HttpClient with the same URL.

How do I change the registration in DI, so that each services get the correct HttpClient injected?

TIA

/Søren

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I would rather do something like this.

public class ServiceA
{ 
    private readonly HttpClient httpClient;

    public ServiceA(HttpClient httpClient)
    { 
        this.httpClient = httpClient;
    }
}
public class ServiceB
{       
    private readonly HttpClient httpClient;
    public ServiceB(HttpClient httpClient)
    {            
        this.httpClient = httpClient;
    }
}

And in ConfigureService.

services.AddHttpClient<ServiceA>().ConfigureHttpClient(client =>
{
    client.BaseAddress = new Uri("http://domainA");
});
services.AddHttpClient<ServiceB>().ConfigureHttpClient(client =>
{
    client.BaseAddress = new Uri("http://domainB");
});

Note :

In your case two things is problamatic.

  1. AddSingleton for ServiceA and ServiceB
  2. AddHttpClient<HttpHelper> is issue as it become singleton and only one get initiated.
over 4 years ago · Santiago Trujillo Report

0

Another way is to set it by factory name:

 //configure your clients services.AddHttpClient("clientName1", client => { //configure }); services.AddHttpClient("clientName2", client => { //configure }); //configure your services services.AddScoped<ServiceA>(sp => { var client = sp.GetRequiredService<IHttpClientFactory>() .CreateClient("clientName1"); return new ServiceA(client); }); services.AddScoped<ServiceB>(sp => { var client = sp.GetRequiredService<IHttpClientFactory>() .CreateClient("clientName2"); return new ServiceB(client); });

One problem with this approach is that you need to explicitly define the resolution for Service A and B.

over 4 years ago · Santiago Trujillo Report

0

I ended up doing this:

sc.AddSingleton(sp =>
{
     var client = sp.GetRequiredService<IHttpClientFactory>()
           .CreateClient(typeof(ServiceA).FullName);
     return new ServiceA(new HttpHelper(client));
 })
 .AddHttpClient<HttpHelper>(typeof(ServiceA).FullName)
 .ConfigureHttpClient((sp, client) => { client.BaseAddress = new Uri("http://domainA"); });

sc.AddSingleton(sp =>
{
     var client = sp.GetRequiredService<IHttpClientFactory>()
                  .CreateClient(typeof(ServiceB).FullName);
     return new ServiceB(new HttpHelper(client));
})
.AddHttpClient<HttpHelper>(typeof(ServiceB).FullName)
.ConfigureHttpClient((sp, client) => { client.BaseAddress = new Uri("http://domainB"); });

seems to work the way I wnated....

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!