Should you dispose HttpResponseMessage when using ReadAsStreamAsync?
Codemia Knowledge Hub ·
Yes, dispose HttpResponseMessage, but only after you have finished reading the stream from ReadAsStreamAsync. The response owns the underlying network stream, so disposing it first closes the connection and truncates or corrupts your data. Put the response and the stream in using scopes that end only after all reading is done.
Dispose after consuming the stream
using var disposes when execution leaves the method. Declaration order matters because contentStream depends on response: response is declared first, so it is disposed last.
Why the response must be disposed
HttpResponseMessage holds resources tied to the HTTP connection:
- The response content stream, which may be backed by a network socket
- Response headers allocated during parsing
- Internal buffers in the HTTP handler pipeline
Without disposal they stay allocated until the garbage collector finalizes the object, which may not happen promptly. In high-throughput code that leaks sockets:
Under load the leaky version eventually throws SocketException or HttpRequestException with "Only one usage of each socket address is normally permitted" because of port exhaustion.
Error paths count too. If EnsureSuccessStatusCode() throws, the response still has to be disposed: declare it with using before the status check, or dispose it in a catch block.
Disposing too early: returning the stream
The most common bug is disposing the response before the stream is consumed, which happens when a method returns the stream:
using var response disposes the response when the method returns. The caller's stream is backed by the response's network connection, which is now closed. ReadAsStreamAsync does not make an independent copy: the stream reads directly from the HTTP pipeline unless you explicitly buffer it.
Returning a stream safely
To hand a stream to the caller, transfer ownership of the response with it.
Wrap the response and stream together
Buffer the content
If the payload fits in memory, copy it into a MemoryStream and dispose the response right away:
This trades memory for simplicity. For large payloads (files, video, data exports), use the wrapper.
HttpCompletionOption and stream lifetime
HttpCompletionOption controls when GetAsync returns.
| Option | Behavior | When to use |
ResponseContentRead (default) | Buffers the entire response body before returning | Small responses where you will read the whole body anyway |
ResponseHeadersRead | Returns as soon as headers are received, body streams lazily | Large responses, file downloads, streaming APIs |
With the default, the whole body is buffered before GetAsync returns, which can cause OutOfMemoryException on large files. With ResponseHeadersRead the connection stays open while you read, so the response must stay undisposed for the entire read.
Dispose the response, not the HttpClient
HttpClient should almost never be disposed per request:
| Resource | Lifetime | Dispose per request? |
HttpClient | Application or service lifetime | No (reuse or use IHttpClientFactory) |
HttpResponseMessage | Single request lifetime | Yes, after consuming the content |
Stream from ReadAsStreamAsync | Tied to the response | Yes, before or alongside the response |
Creating a new HttpClient per request leads to socket exhaustion under load. Keep the client long-lived and dispose only the per-request response.
Related reading
- When should copy-local be set to true and when should it not?
- When should I use a struct instead of a class?
- When should I use async controllers in ASP.NET MVC?
- When should I use GC.SuppressFinalize?
- When should I use LazyT?
- When should I use SnapsToDevicePixels in WPF 4.0?
- When should I use the HashSetT type?
- When should I use using blocks in C?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.