title: Using factory method to create typed HTTP client, ASP.NET Core 2.1
body: <p>I'm following guides/docs on registering HTTP client within my application. There are couple of services I need to call so I decided to go with "Typed clients".</p>
  
  <p>In order to call another service I need to use OAuth - since this is service-to-service call, when I obtain access token, I cache it + I have setup periodical refresh of token. This means there's another component <code>IAccessTokenCache</code> which gives me access token for service.</p>
  
  <p>Thing I'm struggling to figure out is how to register and configure my typed HTTP client providing it also dependency on <code>IAccessTokenCache</code>.</p>
  
  <p>I'm using ASP.NET Core 2.1 (crucial detail, read on).</p>
  
  <p>HTTP client wrapper looks like this (from: <a href="https://www.stevejgordon.co.uk/httpclientfactory-named-typed-clients-aspnetcore" rel="nofollow noreferrer">HttpClientFactory in ASP.NET Core 2.1 (Part 2) </a>):</p>
  
  <pre><code>public class ServiceFooClient
  {
      public ServiceFooClient(HttpClient client)
      {
          Client = client;
      }
  
      public HttpClient Client { get; }
  }
  </code></pre>
  
  <p>And this is how I register and configure client:</p>
  
  <pre><code>services
      .AddHttpClient&lt;ServiceFooClient&gt;(
          c =&gt;
          {
              c.BaseAddress = new Uri("https://www.foo.svc");
  
              // TODO: grab particular access token from cache
              c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "...");
          })
      .AddHttpMessageHandler&lt;ResponseMonitorHandler&gt;()
      .ConfigureHttpMessageHandlerBuilder(
          b =&gt;
          {
              var handler =
                  b.AdditionalHandlers.OfType&lt;ResponseMonitorHandler&gt;().FirstOrDefault();
  
              if (handler != null)
              {
                  handler.ServiceName = "Foo Service";
              }
          });
  </code></pre>
  
  <p>... I'm already configuring HTTP client and even adding my custom HTTP handler. You can see exact point where I want to access <code>IAccessTokenCache</code>, but I can't.</p>
  
  <h1>Possible solutions I can think of:</h1>
  
  <h2>Configure underlying <code>HttpClient</code> in <code>ServiceFooClient</code> wrapper, like:</h2>
  
  <pre><code>// ctor
  public ServiceFooClient(HttpClient httpClient, IAccessTokenCache tokenCache)
  {
      Client = httpClient;
      Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenCache.GetToken("Foo"));
  }
  </code></pre>
  
  <p>This could work nicely, except I don't configuration decoupling - suddenly dedicated HTTP client has part of configuration in <code>Startup</code> (base URI, additional HTTP handler) and another part in wrapping class (setting authorization header).</p>
  
  <h2>Not using <code>AddHttpClient</code> extension method (and others)</h2>
  
  <p>I don't really need to call <code>HttpClientFactoryServiceCollectionExtensions.AddHttpClient&lt;T&gt;(...)</code> - I could do all that stuff myself. But as lazy developer... I don't even want to finish this sentence. There's quite a lot of registering inside, so this is just big no no for me.</p>
  
  <h2>Upgrade to ASP.NET Core 2.2</h2>
  
  <p>Related to previous point - in 2.1, there's no overload of <code>AddHttpClient</code> (2.2: <code>AddHttpClient&lt;TClient&gt;(this IServiceCollection services, Action&lt;IServiceProvider, HttpClient&gt; configureClient)</code>) which would accept callback with service provider. Upgrading to 2.2 would be probably the best solution, yet I will have to be pretty sure that nothing else gets broken (and I already know that there is/was BC break with getting/setting request tracing ID on HTTP context). This could be potentially risky, so I'm trying first to solve my issue in scope of 2.1.</p>
  
  <p>Compare branch of 2.1 with 2.2: <a href="https://github.com/aspnet/HttpClientFactory/blob/master/src/Microsoft.Extensions.Http/DependencyInjection/HttpClientFactoryServiceCollectionExtensions.cs" rel="nofollow noreferrer">HttpClientFactoryServiceCollectionExtensions</a></p>
  
  <h2>Custom HTTP handler setting request headers</h2>
  
  <p>Same way as I now register <code>ResponseMonitorHandler</code>, I could register HTTP handler which has access to <code>IAccessTokenCache</code> and sets request authorization header.</p>
  
  <p>But again, as in first case, this decouples configuration of HTTP client. Also if I had several different access tokens, I would either need to implement several HTTP handlers or do some logic deciding what token from cache to use based on request properties.</p>
  
  <p><strong>Finally, question</strong>: is there any other way I didn't consider? Is there easy solution of this in ASP.NET 2.1? (... apart of just copy-pasting method from 2.2 of course)</p>