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?

gene
  • 2,098
  • 7
  • 40
  • 98
  • 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 '15 at 15:14
  • when are you ever adding the Content.Type to the Response.Header `Response.AddHeader` – MethodMan Aug 31 '15 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 '15 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 '15 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 '15 at 18:34
  • I understand that, but the final result should display PDF file and it does not. Something is definitely not working here, since the same logic is deployed on production server and it works fine – gene Aug 31 '15 at 18:36
  • Then the problem has nothing to do with Disposal, and everything to do with "the PDF file does not display". Please edit your question to state the actual problem. – StingyJack Aug 31 '15 at 18:37
  • Actually, I just found out that it works on production. – gene Aug 31 '15 at 18:39

2 Answers2

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 :(.

Dimenus
  • 61
  • 6
-1

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

Philippe Paré
  • 4,279
  • 5
  • 36
  • 56