title: Register scoped based HttpClient in .NET Core 2
body: <p>I have NET Core 2 Web API application. During the process i have to invoke Client A's API to get some data. So i am using HttpClient to invoke it. Client A also requires me to pass userid and password in header.</p>
  
  <p>So instead of directly injecting <code>HttpClient</code> i have wrapper around <code>HttpClient</code> something like below</p>
  
  <pre><code>public class ClientA : IClientA
  {
      private readonly HttpClient _httpClient;
  
      public ClientA(HttpClient httpClient)
      {
          _httpClient = httpClient;
      }
  
      public async Task&lt;string&gt; GetData()
      {
          return await _httpClient.HttpGetAsync("someurl");
      }
   }
  </code></pre>
  
  <p>Then use ClientA in Service</p>
  
  <pre><code>  public class MyService :IMyService
    {
        private readonly IClientA _clientA
  
        public MyService(IClientA clientA)
        {
             _clientA= clientA
        }
  
        public void DoSomethig()
        {
            _clientA.GetData();
        }           
    }
  </code></pre>
  
  <p>Then i am registering everything in Startup.cs</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.AddScoped&lt;IMyService, MyService&gt;();           
  
          services.AddScoped(factory =&gt;
          {
              Func&lt;Task&lt;IClientA&gt;&gt; provider = async () =&gt;
              {
                  using (var dbContext = factory.GetService&lt;MyDBContext&gt;())
                  {
                      // get userid and password from database here
  
                      var httpClient = new HttpClient();
                      httpClient.DefaultRequestHeaders.Add("UserId",userid);
                      httpClient.DefaultRequestHeaders.Add("Password",password);
                      return new ClientA(httpClient);
                  }
              };
  
              return provider;
          });
      }
  </code></pre>
  
  <p>However i am getting error</p>
  
  <blockquote>
    <p>System.InvalidOperationException: Unable to resolve service for type
    'System.Net.Http.HttpClient' while attempting to activate
    'XXXXXXXXX.ClientA'.    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type
    serviceType, Type implementationType, ISet<code>1 callSiteChain,
    ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type
    serviceType, Type implementationType, ISet</code>1 callSiteChain)</p>
  </blockquote>
  
  <p><em>remaining exception removed for brevity</em></p>
  
  <p>Notice that during registration i am newing-up instance of <code>HttpClient</code> and passing it to ClientA class because i have to set userid and password.    </p>
  
  <p>To get rid the above error I can register HttpClient with UserID and Password with DI framework and i guess that would work.     </p>
  
  <p>However, in that case, if have another client, ClientB, that takes HttpClient then DI framework will inject same httpclient that has userid and password. and that will create security issue because ClientB would see ClientA's credentials in request headers.</p>
  
  <pre><code>   public class ClientB(HttpClient client)
     {
          private readonly _httpClient;
          public class ClientB(HttpClient client)
          {
             _httpClient = client;
          }
  
          public string CallClientB(string url)
          {
              // here ClientB will receive ClientA's credentials
              return await _httpClient.HttpGetAsync(url);
          }
     }
  </code></pre>