c# - 报告查看器和 session 的问题

标签 c# asp.net-mvc-4 session reporting-services reportviewer

我正在使用 c# MVC 4 并嵌入了一个 Report Viewer aspx 页面来呈现来自 SSRS 的报告。我使用的是 SQL Server 2008R2,Microsoft.ReportViewer.WebForms 版本 11.0。

首先是我面临的问题,

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

在我的 web.config 中

注意:此测试场景的超时设置高得离谱。

类似地,我已将 ReportServer 数据库中的 ConfigurationInfo 的关键 SessionTimeout 更新为 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();
            }
        }

我遇到的问题

当我登录时,我在 session 中设置了值。 当我打开一个报表时,该报表在不到一秒的时间内就可以正常执行并显示在页面上。

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

一个 session key 被添加到我的

HttpContext.Current.Session.Keys

此时一切正常,我什至关闭了报告页面。

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

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

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

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

有什么想法吗?

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

提前致谢

最佳答案

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

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

虽然我们无法获得 key 列表,但我们确实可以访问的一件事是 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 block 有助于删除已经过期的报告(因此在访问时抛出异常),而 if 语句中的类型检查将删除尚未过期的报告。

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

关于c# - 报告查看器和 session 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19423030/

相关文章:

php - 结合 HTTP 身份验证和 PHP session ?

session - Express.js + Passport.js : How to restrict multiple login by the same user?

c# - WinForms 多行文本框 : how to paint small image in the corner of TextBox?

c# - 如何选择父级和筛选的子级列表(如果存在)(外连接)

asp.net-mvc - 如何为ASP.NET MVC区域 bundle 资源?

javascript - 我如何让 JS 知道应用程序根目录?

PHP文件显示空白页

c# - 访问静态字符串中的复选框

c# - 如何在 Windows Phone 7 上将 JSON 解析为动态对象?

c# - 指定的转换对字段 int 无效