Don't close my stream!

Post author
Matthew Watkins

When I upgraded to the newest version, I see that the Flush method of the BaseWriter is marked as obsolete. I wondered why I was calling flush explicitly instead of wrapping my writer in a using statement and letting .NET dispose it. Then I saw that I'm explicitly calling Flush because I don't want the underlying stream to be closed on me.

If I were instantiating a StreamWriter myself I would get around this by specifying that I want the base stream to remain open in the StreamWriter's constructor. But it looks like you guys don't have a constructor overload that allows me to pass in a StreamWriter. I assume you guys are constructing a StreamWriter under the covers inside the BaseWriter since your constructor lets us pass in a Stream and an encoding.

Could you add a constructor to the implementations of your writer that either takes in a StreamWriter or else at least lets me specify whether or not I want the stream closed on dispose? In the meantime, I'll just have to live with the little green "obsolete" squiggle... :)

Thanks!

Comments

2 comments

  • Comment author
    Admin

    Hello,

    Which EF Framework version are you using and what .NET version ? When Flush was retired a few versions ago, the writer stream is never closed. You can see here, in the following example:

    using (var stream = new MemoryStream())
    {
    using (var writer = new X12Writer(stream))
    {
    // 3. Begin with ISA segment
    writer.Write(SegmentBuilders.BuildIsa("1"));
    // 4. Follow up with GS segment
    writer.Write(SegmentBuilders.BuildGs("1"));
    // 5. Then write the invoice(s)
    writer.Write(invoice);
    }

    Debug.Write(stream.LoadToString());
    }

    This line:

    Debug.Write(stream.LoadToString());

     

    is executed after the writer was disposed and the stream is still open.

    0
  • Comment author
    Matthew Watkins

    I'm running on .NET 4.6.2 using (as of today) EF 9.7.7. But before today we were using EF 7.3.1. So maybe that's why we had that Flush there. If you don't close the stream for me, that's perfect. Thanks!

    0

Please sign in to leave a comment.