C# - System.ObectDisposedException 在 MemoryStream 中的容量、长度和位置属性
C# - System.ObectDisposedException in MemoryStream for Capacity, Length, and Position Properties
当执行从 Html 流渲染 PDF 文件的代码时,代码似乎执行正常,但未生成 PDF。
通过代码调试时,似乎一切正常,直到我没有开始查看 MemoryStream
对象属性并注意到 MemoryStream
对象下的以下内容:
这是代码:
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();
}
}
}
...
}
执行以下代码行会产生所描述的 MemoryStream 异常。
Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromHtmlStream(myStreamReader.BaseStream, System.Text.Encoding.Default, HttpContext.Current.Request.Url.ToString().Replace(HttpContext.Current.Request.Url.PathAndQuery, "/"));
如果我不使用 Disposable Pattern
,也会出现同样的异常。
相同的代码正在生产中并且工作正常。
可能是什么原因?
果然找到了。在此处阅读文档:
PdfConverter.GetPdfDocumentObjectFromHtmlStream(htmlStream, streamEncoding)
简而言之,它表示关闭您在上面的代码中没有执行的文档对象。
这里也声明了,一旦你完成了它,总是调用文档对象的关闭方法。试试这个更新后的代码。
如果你知道下次使用的库,也加上。一些答案可以在文档中找到(当然不是所有的时间)。
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);
pdfDocument.Close(); // add this line and see what happens
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
...
}
当执行从 Html 流渲染 PDF 文件的代码时,代码似乎执行正常,但未生成 PDF。
通过代码调试时,似乎一切正常,直到我没有开始查看 MemoryStream
对象属性并注意到 MemoryStream
对象下的以下内容:
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();
}
}
}
...
}
执行以下代码行会产生所描述的 MemoryStream 异常。
Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromHtmlStream(myStreamReader.BaseStream, System.Text.Encoding.Default, HttpContext.Current.Request.Url.ToString().Replace(HttpContext.Current.Request.Url.PathAndQuery, "/"));
如果我不使用 Disposable Pattern
,也会出现同样的异常。
相同的代码正在生产中并且工作正常。
可能是什么原因?
果然找到了。在此处阅读文档:
PdfConverter.GetPdfDocumentObjectFromHtmlStream(htmlStream, streamEncoding)
简而言之,它表示关闭您在上面的代码中没有执行的文档对象。
这里也声明了,一旦你完成了它,总是调用文档对象的关闭方法。试试这个更新后的代码。
如果你知道下次使用的库,也加上。一些答案可以在文档中找到(当然不是所有的时间)。
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);
pdfDocument.Close(); // add this line and see what happens
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
...
}