title: Do HttpClient and HttpClientHandler have to be disposed between requests?
body: <p><a href="http://msdn.microsoft.com/en-us/library/hh193681.aspx" rel="noreferrer">System.Net.Http.HttpClient</a> and <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.aspx" rel="noreferrer">System.Net.Http.HttpClientHandler</a> in .NET Framework 4.5 implement IDisposable (via <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpmessageinvoker.aspx" rel="noreferrer">System.Net.Http.HttpMessageInvoker</a>).</p>
  
  <p>The <code>using</code> statement documentation says:</p>
  
  <blockquote>
    <p>As a rule, when you use an IDisposable object, you should declare and
    instantiate it in a using statement.</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/a/12374486/376366">This answer</a> uses this pattern:</p>
  
  <pre><code>var baseAddress = new Uri("http://example.com");
  var cookieContainer = new CookieContainer();
  using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
  using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
  {
      var content = new FormUrlEncodedContent(new[]
      {
          new KeyValuePair&lt;string, string&gt;("foo", "bar"),
          new KeyValuePair&lt;string, string&gt;("baz", "bazinga"),
      });
      cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
      var result = client.PostAsync("/test", content).Result;
      result.EnsureSuccessStatusCode();
  }
  </code></pre>
  
  <p>But the most visible examples from Microsoft don't call <code>Dispose()</code> either explicitly or implicitly. For instance:</p>
  
  <ul>
  <li>The <a href="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/httpclient-is-here.aspx" rel="noreferrer">original blog article</a> announcing the relase of HttpClient.</li>
  <li>The actual <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx" rel="noreferrer">MSDN documentation</a> for HttpClient.</li>
  <li><a href="http://aspnet.codeplex.com/SourceControl/changeset/view/9cb7243bd9fe3b2df484bf2409af943f39533588#Samples/HttpClient/BingTranslateSample/Program.cs" rel="noreferrer">BingTranslateSample</a></li>
  <li><a href="http://aspnet.codeplex.com/SourceControl/changeset/view/9cb7243bd9fe3b2df484bf2409af943f39533588#Samples/HttpClient/GoogleMapsSample/Program.cs" rel="noreferrer">GoogleMapsSample</a></li>
  <li><a href="http://aspnet.codeplex.com/SourceControl/changeset/view/9cb7243bd9fe3b2df484bf2409af943f39533588#Samples/HttpClient/WorldBankSample/Net45/Program.cs" rel="noreferrer">WorldBankSample</a></li>
  </ul>
  
  <p>In the <a href="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/httpclient-is-here.aspx" rel="noreferrer">announcement</a>'s comments, someone asked the Microsoft employee:</p>
  
  <blockquote>
    <p>After checking your samples, I saw that you didn't perform the dispose
    action on HttpClient instance. I have used all instances of HttpClient
    with using statement on my app and I thought that it is the right way
    since HttpClient  implements the IDisposable interface. Am I on the
    right path?</p>
  </blockquote>
  
  <p>His answer was:</p>
  
  <blockquote>
    <p>In general that is correct although you have to be careful with
    "using" and async as they dont' really mix in .Net 4, In .Net 4.5 you
    can use "await" inside a "using" statement.</p>
    
    <p>Btw, you can reuse the same HttpClient as many times are [as] you like so
    typically you won't create/dispose them all the time.</p>
  </blockquote>
  
  <p>The second paragraph is superfluous to this question, which is not concerned about how many times you can use an HttpClient instance, but about if it is necessary to dispose it after you no longer need it.</p>
  
  <p>(Update: in fact that second paragraph is the key to the answer, as provided below by @DPeden.)</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>Is it necessary, given the current implementation (.NET Framework 4.5), to call Dispose() on HttpClient and HttpClientHandler instances? Clarification: by "necessary" I mean if there are any negative consequences for not disposing, such as resource leakage or data corruption risks.</p></li>
  <li><p>If it's not necessary, would it be a "good practice" anyway, since they implement IDisposable?</p></li>
  <li><p>If it's necessary (or recommended), is <a href="https://stackoverflow.com/a/12374486/376366">this code</a> mentioned above implementing it safely (for .NET Framework 4.5)?</p></li>
  <li><p>If these classes don't require calling Dispose(), why were they implemented as IDisposable?</p></li>
  <li><p>If they require, or if it's a recommended practice, are the Microsoft examples misleading or unsafe?</p></li>
  </ol>