我们当前正在使用 Windows 服务来生成 PDF 文件。我最近正在优化代码并注意到内存的滥用。这是由于缺乏一个using周围的声明var reportViewer = new ReportViewer()
但是,添加此代码后,代码在关闭后引发了运行时空引用异常using块,源自内部的代码Dispose method.
为什么会出现此错误以及如何正确处理该对象?
答案可以在 Visual Basic 中找到here http://blog.700cb.net/index.php/2015/03/13/ssrs-reportviewer-nullreferenceexception-on-dispose/是因为由于某种原因 ReportViewer 需要 HttpContext。如果不存在,则会发生此错误。
作为解决方法,可以添加以下行来解决该问题:
if (System.Web.HttpContext.Current == null)
{
System.Web.HttpContext.Current = new System.Web.HttpContext(
new System.Web.HttpRequest(System.IO.Path.GetRandomFileName(), "https://www.stackoverflow.com", string.Empty),
new System.Web.HttpResponse(System.IO.TextWriter.Null)
);
}
这将创建一个假的 HttpContext,允许以某种方式正确处理 ReportViewer 实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)