title: How to correctly instantiate a static HttpClient in constructor
body: <p>I have a custom <a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netstandard-2.0" rel="nofollow noreferrer">HttpClient</a> implementation that needs some values from my configuration to work properly.</p>
  
  <pre><code>public class MyHttpClient : HttpClient
  {
      public MyHttpClient()
      {
      }
  
      public MyHttpClient(string token)
      {
          Token = token;
      }
  
      public string Token { get; set; }
  
      // ...
  }
  </code></pre>
  
  <p>I now want to use this HttpClient in my application logic that uses <code>IOptions&lt;TOptions&gt;</code> for getting its configuration.</p>
  
  <pre><code>public class MyLogic
  {
      private static MyHttpClient Client;
  
      public MyLogic(IOptions&lt;MyConfig&gt; config)
      {
          // How to instantiate Client with values from config?
      }
  }
  </code></pre>
  
  <p>Since HttpClient <a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netstandard-2.0#remarks" rel="nofollow noreferrer">should be held as a static field</a>, I struggle with the method I can inject the config value into the HttpClient. I came up with a few possibilities, but none of them seems to be fine.</p>
  
  <p><strong>Option 1</strong>: <em>set config value each time</em></p>
  
  <pre><code>public class MyLogic
  {
      private static MyHttpClient Client = new MyHttpClient();
  
      public MyLogic(IOptions&lt;MyConfig&gt; config)
      {
          Client.Token = config.Value.Token;
      }
  }
  </code></pre>
  
  <p>This seems to be the worst option, since the value of <code>Token</code> may change as the HttpClient uses it. </p>
  
  <p><strong>Option 2</strong>: <em>lock each time</em></p>
  
  <pre><code>public class MyLogic
  {
      private static MyHttpClient Client;
      private static readonly object ClientLock = new object();
  
      public MyLogic(IOptions&lt;MyConfig&gt; config)
      {
          lock(ClientLock)
          {
              if (Client == null)
              {
                  Client = new MyHttpClient(config.Value.Token);
              }
          }
      }
  }
  </code></pre>
  
  <p>This seems to be bad from a performance perspective, since each instantiation of <code>MyLogic</code> would create a lock and block all others.</p>
  
  <p><strong>Option 3</strong>: <em>double-checked locking</em></p>
  
  <pre><code>public class MyLogic
  {
      private static volatile MyHttpClient Client;
      private static readonly object ClientLock = new object();
  
      public MyLogic(IOptions&lt;MyConfig&gt; config)
      {
          if (Client == null)
          {
              lock(ClientLock)
              {
                  if (Client == null)
                  {
                      Client = new MyHttpClient(config.Value.Token);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This seems like the solution, however double-checked locks are hard to get right and should therefore be avoided.</p>
  
  <p>Sadly, <a href="https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1?view=netstandard-2.0" rel="nofollow noreferrer">Lazy&lt;T></a> doesn't seem to be an option since it also requires a static context for instance creation.</p>
  
  <p>So, what would be the best option to instantiate the HttpClient in this situation?</p>