Skip to content

Instantly share code, notes, and snippets.

@danielmarbach
Created January 11, 2013 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmarbach/4508795 to your computer and use it in GitHub Desktop.
Save danielmarbach/4508795 to your computer and use it in GitHub Desktop.
"Microsoft.Usage", "CA2202:Do not dispose objects multiple times"
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream) { AutoFlush = true })
{
writer.Write(message.Xml);
stream.Seek(0, SeekOrigin.Begin);
this.documentSession.StoreAttachement(
"someId", null, stream, new RavenJObject { { "Content-Type", "application/xml" }, });
}
@mtrainham
Copy link

If I had to guess, I'd say stream is being disposed within writer first when writer is disposed, and then again after stream's using statement ends, so just create the stream without a using statement.

using (var writer = new StreamWriter(new MemoryStream()) { AutoFlush = true })
{
    writer.Write(message.Xml);

    stream.Seek(0, SeekOrigin.Begin);

    this.documentSession.StoreAttachement(
        "someId", null, stream, new RavenJObject { { "Content-Type", "application/xml" }, });
 }

@hedoluna
Copy link

hedoluna commented Dec 9, 2014

It's equal to the official MSDN documentation: http://msdn.microsoft.com/it-it/library/ms182334.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment