The thing I am unsure about is whether the BinaryWriter and BinaryReader streams from the private function will be disposed of when the MemoryStream is when the public method loses scope?
The thing is, BinaryWriter
and BinaryReader
are not streams.
They act upon the underlying stream which, in your case, is the memoryStream
.
So, as long as you close your memoryStream
and the outer responseStream
, everything is fine.
Edit:
In fact, take a look at their source code:
protected virtual void Dispose(bool disposing)
{
if (disposing)
OutStream.Close();
}
As you can see, all it does is call Dispose
on the underlying stream. Your current code already does that - so there's no need to dispose of the writer/reader. It would be redundant.