2

我在 Web 应用程序中使用 VS2010 Report Viewer 控件。应用程序会话状态模式设置为 StateServer 如下

    <sessionState timeout="30" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" />

reportviewer 控件在我的开发机器上工作正常,但是当应用程序部署到服务器上并且加载 reportviewer 控制页面时,会引发以下错误。所有其他页面工作正常。

“无法序列化会话状态。在 'StateServer' 和 'SQLServer' 模式下,ASP.NET 将序列化会话状态对象,因此不允许不可序列化的对象或 MarshalByRef 对象。如果类似的序列化,同样的限制适用由自定义会话状态存储在“自定义”模式下完成。”

任何人都可以请帮助,任何想法都会有很大帮助..

提前致谢。


rptvw.ProcessingMode = ProcessingMode.Remote;
        rptvw.ServerReport.ReportServerUrl = new Uri("http://localhost:90/reportserver");
        rptvw.ServerReport.ReportPath = string.Format("/Reports/{0}", reportName);

        var param = new ReportParameter[4];

        param[0] = new ReportParameter("Parameter1", DropDownListCodes.SelectedValue));
        param[1] = new ReportParameter("Parameter2", DropDownListQuarters.SelectedValue));
        param[2] = new ReportParameter("Parameter3", DropDownListComparators.SelectedValue));
        param[3] = new ReportParameter("Parameter4", comptype);

        rptvw.ServerReport.SetParameters(param);

        rptvw.ServerReport.Refresh();
4

1 回答 1

2

我设法让它工作。我按照此链接获取我的解决方案msdn 链接

“在实现 IReportServerCredentials 接口时,重要的是要知道 ReportViewer 控件将对象的实例存储在 ASP.NET 会话中。如果服务器的 ASP.NET 会话存储在进程外,例如在 Reporting Services 中,则该类必须被标记为可序列化,以便它可以被序列化以进行存储。” 取自上面的链接。

在 App_Code\ReportServerConnection.cs 中创建了一个新文件

    [Serializable]
    public sealed class ReportServerConnection : IReportServerConnection2
    {
        public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;

            // Not using form credentials
            return false;
        }

        public WindowsIdentity ImpersonationUser
        {
            // Use the default Windows user.  Credentials will be
            // provided by the NetworkCredentials property.
            get { return null; }
        }

        public ICredentials NetworkCredentials
        {
            get
            {
                // Read the user information from the web.config file. By reading the information on demand instead of 
                // storing it, the credentials will not be stored in session, reducing the vulnerable surface area to the
                // web.config file, which can be secured with an ACL.

                // User name
                string userName = ConfigurationManager.AppSettings["ReportViewerUser"];

                if (string.IsNullOrEmpty(userName))
                    throw new InvalidOperationException("Please specify the user name in the project's Web.config file.");

                // Password
                string password = ConfigurationManager.AppSettings["ReportViewerPassword"];

                if (string.IsNullOrEmpty(password))
                    throw new InvalidOperationException("Please specify the password in the project's Web.config file");

                // Domain
                string domain = ConfigurationManager.AppSettings["ReportViewerDomain"];

                if (string.IsNullOrEmpty(domain))
                    throw new InvalidOperationException("Please specify the domain in the project's Web.config file");

                return new NetworkCredential(userName, password, domain);
            }
        }

        public Uri ReportServerUrl
        {
            get
            {
                string url = ConfigurationManager.AppSettings["ReportServerUrl"];

                if (string.IsNullOrEmpty(url))
                    throw new InvalidOperationException("Please specify the report server URL in the project's Web.config file");

                return new Uri(url);
            }
        }

        public int Timeout
        {
            // set timeout to 60 seconds
            get { return 60000; }
        }

        public IEnumerable<Cookie> Cookies
        {
            // No custom cookies
            get { return null; }
        }

        public IEnumerable<string> Headers
        {
            // No custom headers
            get { return null; }
        }
    }

在 Report.aspx.cs 页面上

    protected void Page_Init(object sender, EventArgs e)
    {
        rptvw.ServerReport.ReportServerCredentials = new ReportServerConnection();
    }

在主帖的代码中更改了这一行 rptvw.ServerReport.ReportServerUrl = rsc.ReportServerUrl;

并在 Web.config

<appSettings>
<add key="ReportViewerServerConnection" value=" App_Code.ReportServerConnection, App_Code"/>
<add key="ReportViewerUser" value="username"/>
<!-- Used as the user name by the ReportServerConnection class. -->
<add key="ReportViewerPassword" value="password"/>
<!-- Used as the password by the ReportServerConnection class. -->
<add key="ReportViewerDomain" value="domainname"/>
<!-- Used as the domain by the ReportServerConnection class. -->
<add key="ReportServerUrl" value="http://localhost:90/reportserver"/>
<!-- Used as the report server URL by the ReportServerConnection class. -->
于 2012-10-08T09:13:50.310 回答