3

我正在使用 c# MVC 4 并嵌入了一个报表查看器 aspx 页面来呈现来自 SSRS 的报表。我正在使用 Sql Server 2008R2,Microsoft.ReportViewer.WebForms 版本 11.0。

首先我面临的问题,

我在项目中使用会话变量来保存相对于站点的值。这些与 SSRS 无关,例如 UserId。

在 web.config 我有

注意:对于这个测试场景,超时设置得非常高。

同样,我已将 ReportServer 数据库中的 SessionTimeout 键的 ConfigurationInfo 更新为 60(又是一个荒谬的测试值)。

我的 ReportViewer 代码如下打开:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ReportViewer1.Reset();
                string Reportpath = Request["ReportName"];
                string ServerUserName = ConfigurationManager.AppSettings["ReportServerUser"];
                string ServerPassword = ConfigurationManager.AppSettings["ReportServerPwd"];
                string ReportServerDomain = ConfigurationManager.AppSettings["ReportServerDomain"];
                string ReportsPath = ConfigurationManager.AppSettings["ReportsPath"];
                ReportViewer1.ProcessingMode = ProcessingMode.Remote;

                // Get report path from configuration file
                ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServer"]);
                ReportViewer1.ServerReport.ReportPath = String.Format(ReportsPath + "/" + Reportpath);

                IReportServerCredentials irsc = new CustomReportCredentials(ServerUserName, ServerPassword, ReportServerDomain);
                ReportViewer1.ServerReport.ReportServerCredentials = irsc;
                ReportViewer1.ShowPrintButton = false;

                #region Parameters for report
                Microsoft.Reporting.WebForms.ReportParameter[] reportParameterCollection = new Microsoft.Reporting.WebForms.ReportParameter[1];
                reportParameterCollection[0] = new Microsoft.Reporting.WebForms.ReportParameter();
                reportParameterCollection[0].Name = "Example";
                reportParameterCollection[0].Values.Add("Example");

                ReportViewer1.ServerReport.SetParameters(reportParameterCollection);
                #endregion Parameters for report
                ReportViewer1.ServerReport.Refresh();
            }
        }

我面临的问题

当我登录时,我在会话中设置了值。当我打开报告时,报告会在一秒钟内正常执行并显示在页面上。

在幕后,我注意到报表服务器临时数据库中插入了一行,过期日期为 1 分钟后(使用我的配置)。

会话密钥被添加到我的

HttpContext.Current.Session.Keys

此时一切都很好,我什至关闭了报告页面。

此时我等待一分钟,这意味着会话在 ReportServerTempDB 中过期。

然后我导航到一个页面,该页面的操作使用 HttpContext.Current.Session 作为值。同样,此操作与报告无关。但是,当它尝试检索密钥时,出现以下错误

Microsoft.Reporting.WebForms.ReportServerException: The report execution <key> has expired or cannot be found. (rsExecutionNotFound)

我一直在谷歌搜索,但没有找到解决我的问题的有效解决方案,因为大多数人似乎都遇到过长时间运行的报告,其中报告会话在执行完成之前就超时了。

有任何想法吗?

如果需要更多信息,请发表评论,我会更新问题。

提前致谢

4

2 回答 2

3

我遇到了同样的问题。看来,当 ReportViewer 放入 Session 的对象被访问时,它们会尝试 ping 报表服务器。如果报告已过期,则会引发 ReportServerException,这是用户仍在查看过期报告时的预期行为,但当他们不再在该页面上时则不会。

不幸的是,这些 Session 条目在用户离开包含 ReportViewer 的页面后不会被清除。并且由于某种原因,访问 Session 中的键列表将触发报表服务器被 ping,从而在完全不相关的页面上引发此异常。

虽然我们无法获得密钥列表,但我们可以访问的一件事是 Session 中的条目数。使用此信息,我们可以遍历 Session 中的每个项目并确定它是否是与 ReportViewer 关联的项目之一,然后将其删除。

[WebMethod]
public static void RemoveReportFromSession()
{
    var i = HttpContext.Current.Session.Count - 1;

    while (i >= 0)
    {
        try
        {
            var obj = HttpContext.Current.Session[i];
            if (obj != null && obj.GetType().ToString() == "Microsoft.Reporting.WebForms.ReportHierarchy")
                HttpContext.Current.Session.RemoveAt(i);
        }
        catch (Exception)
        {
            HttpContext.Current.Session.RemoveAt(i);
        }

        i--;
    }
}

catch 块有助于删除已经过期的报告(因此在访问时会抛出异常),而 if 语句中的类型检查将删除尚未过期的报告。

我建议从包含您的 ReportViewer 的页面的 onunload 事件中调用它。

于 2016-03-16T19:41:02.943 回答
0

关于 Patrick J. Brown 的回答要考虑的事情:在 try 块中删除 Session 值之前要三思而后行,可能属于其他浏览器窗口/选项卡。

于 2021-10-29T08:48:09.377 回答