开发者

Session state mode set to SQLSERVER getting error

开发者 https://www.devze.com 2023-04-04 10:04 出处:网络
I want to store my session in SQL Server database and for that I want to use session state mode SQLSERVER, and I have changed my web.config and added the session state attribute:

I want to store my session in SQL Server database and for that I want to use session state mode SQLSERVER, and I have changed my web.config and added the session state attribute:

<sessionState mode="SQLServer" timeout="30" sqlConnectionString="Data Source=server;user id=sa;password=sa;" cookieless="false" />

When I run my application I get this error:

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.

Please h开发者_运维知识库elp me, I am in serious problem. Thanks & Regards.


The question is old but had no valid answer, I had same problem and found the solution on another site. Since this topic comes as first result on google I am adding the solution here for others.

The issue is about adding a struct object into session. ASP.NET web application handles the storage of session objects differently from InProc mode to SQLServer mode. When storing session objects into a SQL Server, .NET framework will serialize the objects. This is necessary because the session object needs to be transferred from server to server.

Stack trace (yellow section on the error page) shows exactly which object caused the problem. In my case:

[SerializationException: Type 'admin+users+stUsers' in Assembly 'App_Code.3_0rnsep, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.]

I added [Serializable] tag ebove my struct and the problem was gone.

[Serializable]
public struct stUsers
{ .... }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号