I have the following code that uses a stream to open and modify an Open XML document, and then save a new binary representation of this stream:
MemoryStream stream = null; try { stream = new MemoryStream(); stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length); using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true)) { OfficeDocument.ModifyDocument(document); this.SetBinaryRepresentation(stream.ToArray()); stream = null; } } finally { if (stream != null) { stream.Dispose(); } }
I initially used the two blocks used (one for MemoryStream and one for WordprocessingDocument), but I got warning CA2202: "Object" thread "can be deleted more than once in a method ..." In the MSDN article , I changed the code above (converting external use per attempt), but I still get this warning.
I'm not sure how I can structure this method to ensure that Dispose is called exactly once in the stream. I would prefer not just to suppress this warning, as the MSDN article states that you should not rely on Dispose, which can be safely called multiple times.
source share