title: Using HttpClient in external library for ASP.NET Core
body: <p><strong>Introduction</strong></p>
  <p>I am developing SDK (that is just a wrapper for an REST API). This SDK is internally using the HttpClient to make requests for the API. The SDK targets .NET Standard 2.1. Here is simplified code.</p>
  <pre class="lang-cs prettyprint-override"><code>public class SomeSdk 
  {
      private readonly HttpClient _httpClient;
  
      public SomeSdk(&lt;some-settings&gt;) 
      {
          _httpClient = new HttpClient(&lt;some-settings&gt;);
      }
  
      public async Task&lt;string&gt; GetSomethingFromApiAsync() 
      {
          return await _httpClient.GetStringAsync(...);
      }
  }
  </code></pre>
  <p><strong>Issue</strong></p>
  <p>The SDK is being used by other developers in ASP.NET Core projects. I am aware of the troubles with HttpClient and I know that MS DOCS recommand to inject HttpClientFactory to avoid SocketException and to reflect DNS changes.</p>
  <p>First solution that comes to mind is to let the user pass the HttpClient (that is from HttpClientFactory) in the SDK constructor or other methods. Like this.</p>
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt;string&gt; GetSomethingFromApiAsync(HttpClient clientFromFactory) 
  {
      return await clientFromFactory.GetStringAsync(...);
  }
  </code></pre>
  <p>However there is problem that the SDK configures the HttpClient before it uses it (Proxy, Timeout, etc.) Therefore it's initialization should be left in the SDK constructor.</p>
  <p><strong>Question</strong></p>
  <p>My current solution is to inject SDK as singleton in the ASP.NET Core projects and accept the DNS reflection issues. I would still like to ask if there is anyone who would recommand me a better approach for this problem.</p>
  <p>Thank you very much, Adam.</p>