title: When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?
body: <p>I am using the <a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx" rel="noreferrer"><code>System.Net.Http.HttpClient</code></a> to do some client-side HTTP communication. I've got all of the HTTP in one spot, abstracted away from the rest of the code. In one instance I want to read the response content as a stream, but the consumer of the stream is well insulated from where the HTTP communication happens and the stream is opened. In the spot responsible for HTTP communication I am disposing of all of the <code>HttpClient</code> stuff.</p>
  
  <p>This unit test will fail at <code>Assert.IsTrue(stream.CanRead)</code>:</p>
  
  <pre><code>[TestMethod]
  public async Task DebugStreamedContent()
  {
      Stream stream = null; // in real life the consumer of the stream is far away 
      var client = new HttpClient();        
      client.BaseAddress = new Uri("https://www.google.com/", UriKind.Absolute);
  
      using (var request = new HttpRequestMessage(HttpMethod.Get, "/"))
      using (var response = await client.SendAsync(request))
      {
          response.EnsureSuccessStatusCode();
          //here I would return the stream to the caller
          stream = await response.Content.ReadAsStreamAsync();
      }
  
      Assert.IsTrue(stream.CanRead); // FAIL if response is disposed so is the stream
  }
  </code></pre>
  
  <p>I typically try to dispose of anything <code>IDisposable</code> at the earliest possible convenience but in this case, disposing the <code>HttpResponseMessage</code> also disposes the <code>Stream</code> returned from <code>ReadAsStreamAsync</code>.</p>
  
  <p>So it seems like the calling code needs to know about and take ownership of the response message as well as the stream, or I leave the response message undisposed and let the finalizer deal with it. Neither option feels right. </p>
  
  <p><a href="https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed">This answer</a> talks about not disposing the <code>HttpClient</code>. How about the <code>HttpRequestMessage</code> and/or <code>HttpResponseMessage</code>?</p>
  
  <p>Am I missing something? I am hoping to keep the consuming code ignorant of HTTP  but leaving all these undisposed objects around goes against year of habit!</p>