1

I'm using a Disposable pattern when generating PDF file. The following code is used:

public partial class WriteNotes : System.Web.UI.Page
{
     ...
     protected override void Render(System.Web.UI.HtmlTextWriter writer)
     {
        ...
        using (System.IO.MemoryStream printStream = new System.IO.MemoryStream())
        using (System.IO.StreamWriter printStreamWriter = new System.IO.StreamWriter(printStream))
        using (System.Web.UI.HtmlTextWriter printWriter = new System.Web.UI.HtmlTextWriter(printStreamWriter))
        {
            base.Render(printWriter);
            printWriter.Flush();
            using (System.IO.StreamReader myStreamReader = new System.IO.StreamReader(printStream))
            {
               myStreamReader.BaseStream.Position = 0;
               Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromHtmlStream(myStreamReader.BaseStream, System.Text.Encoding.Default, HttpContext.Current.Request.Url.ToString().Replace(HttpContext.Current.Request.Url.PathAndQuery, "/"));
               HttpContext.Current.Response.Clear();
               HttpContext.Current.Response.ContentType = "application/pdf";
               pdfDocument.Save(HttpContext.Current.Response.OutputStream);
               HttpContext.Current.Response.Flush();
               HttpContext.Current.Response.End();
            }
        }
    }
    ...
}

After executing:

Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromHtmlStream(myStreamReader.BaseStream,   System.Text.Encoding.Default,HttpContext.Current.Request.Url.ToString().Replace(HttpContext.Current.Request.Url.PathAndQuery, "/"));

I observe the following when go through the MemoryStream's properties:

Capacity: 'printStream.Capacity' threw an exception of type 'System.ObjectDisposedException'
Length: 'printStream.Length' threw an exception of type 'System.ObjectDisposedException'
Position: 'printStream.Position' threw an exception of type 'System.ObjectDisposedException'

What can possibly be wrong with the code?

8
  • if you are looking at those properties in one of the debugger windows (watch, auto, locals), they can sometimes fail to evaluate. It doesn't necessarily mean there is something wrong with the code.
    – StingyJack
    Aug 31, 2015 at 15:14
  • when are you ever adding the Content.Type to the Response.Header Response.AddHeader
    – MethodMan
    Aug 31, 2015 at 15:14
  • I have placed your suggested line of code printStream.Seek(0, SeekOrigin.Begin) right after printWriter.Flush() and before using (System.IO.StreamReader myStreamReader = new System.IO.StreamReader(printStream)), however, I still have the same problem
    – gene
    Aug 31, 2015 at 15:27
  • @StingyJack Does not really matter. Even If I do not use Disposable it gives the same error. Not sure what is wrong
    – gene
    Aug 31, 2015 at 15:54
  • it has nothing to do with using IDisposable or not. The debugger cannot always display reliable information, and this is especially problematic when inspecting objects.
    – StingyJack
    Aug 31, 2015 at 18:34

2 Answers 2

0

Maybe the compiler isn't bracketing the using blocks correctly. Are you seeing the same issue if you explicitly bracket the using statements out?

Edit: Can't post as comment due to lack of rep :(.

2
  • 1
    this should be a comment not an answer.. remove it and spare yourself the potential down votes..!
    – MethodMan
    Aug 31, 2015 at 15:14
  • 1
    I don't have 50 rep yet. Can't comment.
    – Dimenus
    Aug 31, 2015 at 15:40
-1

The problem is, you did flush the stream before moving to the position = 0. Try without flushing, just comment out the printWriter.Flush().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.