title: Having dynamic proxy with HttpClientFactory implementation
body: <p>I have <code>Asp.Net Core WebApi</code>. I am making Http requests according to <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1" rel="noreferrer">HttpClientFactory pattern</a>. Here is my sample code: </p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      ...
      services.AddHttpClient&lt;IMyInterface, MyService&gt;();
      ...
  }
  
  public class MyService: IMyInterface
  {
      private readonly HttpClient _client;
  
      public MyService(HttpClient client)
      {
          _client = client;
      }
  
      public async Task CallHttpEndpoint()
      {
          var request = new HttpRequestMessage(HttpMethod.Get, "www.customUrl.com");
          var response = await _client.SendAsync(request);
          ...
      }
  
  }
  </code></pre>
  
  <p>I want to implement sending requests through dynamic proxy. This basically means that I might need to change proxy with each request. As for now I find out 2 approuces, non of which seems good to me:</p>
  
  <p>1.Have a static proxy like this: </p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      ...
      services.AddHttpClient&lt;IMyInterface, MyService&gt;().ConfigurePrimaryHttpMessageHandler(() =&gt;
          {
              return new HttpClientHandler
              {
                  Proxy = new WebProxy("http://127.0.0.1:8888"),
                  UseProxy = true
              };
          });
      ...
  }
  </code></pre>
  
  <p>But I can only have single proxy per service in this approach.</p>
  
  <p>2.Dispose <code>HttpClient</code> with each request:</p>
  
  <pre><code>    HttpClientHandler handler = new HttpClientHandler()
      {
          Proxy = new WebProxy("http://127.0.0.1:8888"),
          UseProxy = true,
      };
  
      using(var client = new HttpClient(handler))
      {
          var request = new HttpRequestMessage(HttpMethod.Get, "www.customUrl.com");
          var response = await client.SendAsync(request);
          ...
      }
  </code></pre>
  
  <p>But in this way I violate HttpClientFactory pattern and it might cause issues to application performance as stated in following <a href="https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/" rel="noreferrer">article</a></p>
  
  <p>Is there a third way where I could change proxy dinamically without re-creating <code>HttpClient</code>?</p>