Search code examples
asp.netreporting-servicesreportviewerreport-viewer2010

ReportViewer - "Unable to serialize the session state. in 'stateserver' and 'sqlserver' mode


I am using VS2010 Report Viewer control in web application. The applications sessionstate mode is set to StateServer as follows

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

The reportviewer control is working fine on my devlopment machine but when the applicaiton is deployed onto server and when the reportviewer control page is loaded the following error is thrown.. All the other pages are working fine.

"Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode."

Can anyone please help, any idea will be of great help..

Thanks in advance.


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();

Solution

  • I managed to get it to work. I followed this link for my solution msdn link

    "When implementing the IReportServerCredentials interface, it is important know that the ReportViewer control stores the instance of the object in ASP.NET session. If the server's ASP.NET session is being stored out of process, such as in Reporting Services, the class must be marked Serializable so that it may be serialized for storage." taken from above link.

    Created a new file in 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; }
            }
        }
    

    On the Report.aspx.cs page

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

    Changed this line in the code on the main post rptvw.ServerReport.ReportServerUrl = rsc.ReportServerUrl;

    And in the 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. -->