title: What is the best practice to configure httpClients in ASP Net Core 3.0 during startup?
body: <p>ASP NET Core 2.1 gave us HttpClientFactory which is a great way to build custom httpClients.</p>
  
  <p>We used this method together with an endpoint configuration service that was injected into the constructor of the startup.cs class.</p>
  
  <p>The code looked somewhat like:</p>
  
  <pre><code>        public Startup(IConfiguration configuration, IEndpointService endpointService)
          {
              Configuration = configuration;
              _endpointService = endpointService;
          }
  
          public void ConfigureServices(IServiceCollection services)
          {
             foreach(var endpoint in _endpointService)
             {
                services.AddHttpClient(endpoint.name, c =&gt; {
                   c.BaseAddress = endpoint.Address;
                   // plus other config as required
                });
             }
          }
  </code></pre>
  
  <p>IEndpointService was configured prior to Startup being injected with the following call.</p>
  
  <pre><code>    IWebHostBuilder webHostBuilder =
          WebHost.CreateDefaultBuilder(args)
              .ConfigureServices((webHostBuilderContext, services) =&gt; {
                  services.AddSingleton&lt;IEndpointService, EndpointService&gt;();
              })
              .UseStartup&lt;Startup&gt;();
  </code></pre>
  
  <p>This allowed us to read information from an external data source and configure multiple named httpClients with differing configuration.</p>
  
  <p>This feature has been removed in ASP NET Core 3.0.</p>
  
  <p>See: <a href="https://weblogs.thinktecture.com/pawel/2017/08/aspnet-core-beware-singleton-may-not-be-singleton.html" rel="nofollow noreferrer">https://weblogs.thinktecture.com/pawel/2017/08/aspnet-core-beware-singleton-may-not-be-singleton.html</a> and <a href="https://andrewlock.net/avoiding-startup-service-injection-in-asp-net-core-3/" rel="nofollow noreferrer">https://andrewlock.net/avoiding-startup-service-injection-in-asp-net-core-3/</a></p>
  
  <p>This leaves us with a question how best to achieve the same functionality in 3.0.</p>
  
  <p>This is the best I've come up with so far but I'm keen to know what the guidance is on this issue.</p>
  
  <pre><code>        public void ConfigureServices(IServiceCollection services)
          {
              services.AddSingleton&lt;IEndpointService, EndpointService&gt;();
  
              using (var serviceProvider = services.BuildServiceProvider())
              {
                  var endpointService = serviceProvider.GetService&lt;IEndpointService&gt;();
  
                  foreach (var endpointConfiguration in endpointService.GetEndpointConfiguration())
                  {
                      // Create a named client and any other configuration that we need to do here.
                      services.AddHttpClient(endpointConfiguration.Name, client =&gt;
                      {
                          client.BaseAddress = endpointConfiguration.BaseAddress;
  
                          // Add any extra config that is required here
                      });
                  }
              }
          }
  </code></pre>
  
  <p>In this example I force creation of the EndpointService using serviceProvider.GetService.</p>
  
  <p>I'm conscious of the fact that I've probably recreated the multiple singleton bug (see links above), but am unsure of the best approach here.</p>
  
  <p>Thoughts ?</p>