title: Setting up HttpClient in .NET to make it work with multiple threads and to provide concurrency
body: <p>After reading the posts below about recommended usage of <code>HttpClient</code>, I changed my code from instantiating <code>HttpClient</code> per request within a using block to a long-lived object. </p>
  
  <p><a href="https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed">Do HttpClient and HttpClientHandler have to be disposed?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/22560971/what-is-the-overhead-of-creating-a-new-httpclient-per-call-in-a-webapi-client">What is the overhead of creating a new HttpClient per call in a WebAPI client?</a></p>
  
  <p>My implementation is part of a low-level api, and would be to make requests from from different parts of the app running on different threads, so thread-safety and concurrency when making requests needs to be guaranteed as well.</p>
  
  <p>I even went on to make it a singleton as below, so there is just one instance of HttpClient used throughout the app. (fourth version form John SKeet's article)</p>
  
  <p><a href="http://csharpindepth.com/Articles/General/Singleton.aspx" rel="nofollow noreferrer">http://csharpindepth.com/Articles/General/Singleton.aspx</a></p>
  
  <pre><code>public sealed class MyHttpClient
  {       
      private static readonly volatile HttpClient _myHttpClient = new HttpClient();
  
      static MyHttpClient() {}
      private MyHttpClient(){ }
  
      public static HttpClient MyHttpClientObj
      {
          get
          {               
              return _myHttpClient;
          }
      }
  }
  </code></pre>
  
  <p>And below is an example of how this gets used</p>
  
  <pre><code>  public IEnumerable&lt;string&gt; GetSomeData(string url, FormUrlEncodedContent bodyParameters)
    {
        try
        {
            //is it possible to configure timeout here instead, such that every request will have its one timeout duration?
            var response = MyHttpClient.MyHttpClientObj.PostAsync(url, bodyParameters);
  
            var result = response.Result;
  
            if (!result.IsSuccessStatusCode)
            {
               //log and return null
            }
  
            var data = JsonConvert.DeserializeObject&lt;List&lt;string&gt;&gt;(result.Content.ReadAsStringAsync().Result);
  
            return data;
  
        }
        catch (Exception ex)
        {
            //logging exceptions
        }
    }
  </code></pre>
  
  <p>When making requests via <code>HttpClient</code>, I've made sure to use only the therad-safe methods listed below, but when deserializing the response, <code>result.Content.ReadAsStringAsync().Result</code> is used. This is because the higher level calls don't support async responses yet.</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx#Anchor_5" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx#Anchor_5</a></p>
  
  <p>However, I still have a few questions. </p>
  
  <ol>
  <li>Is this approach Thread-safe and stable enough to not cause any memory leaks?</li>
  <li>How can I configure the Timeout for every request?</li>
  <li>Is it necessary to specify 'Connection: keep-alive' in DefaultHeaders?</li>
  <li>How can I add a custom header/modify a default header for every request?</li>
  <li>And finally, Are there any known performance issues/drawbacks in using HttpClient this way?</li>
  </ol>