无法序列化会话状态

Sch*_*oof 31 c# asp.net session

将我的应用程序放在Web服务器上并尝试"登录"时出现以下错误:

Server Error in '/' Application.
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.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SerializationException: Type 'Gebruiker' in Assembly 'App_Code.qzuhycmn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.]
   System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +9452985
   System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +247
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +160
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +218
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +54
   System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +542
   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +133
   System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1708

[HttpException (0x80004005): 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.]
   System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1793
   System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +34
   System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +638
   System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +244
   System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +67
   System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +114
   System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +807
   System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs) +184
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 
Run Code Online (Sandbox Code Playgroud)

我在Sessions做的唯一一件事就是:

  • Session ["Rechten"] ="GlobaalAdmin"; (例)
  • Session ["gebruikerid"] = txtID.Text; (他们登录的ID)
  • 并将它们重定向到另一个页面:Response.Redirect("LoggedIn.aspx",true);
  • http://pastebin.com/jZprwA8m(将gebruiker放入会话中)

关于这个问题已有多个主题,但它们似乎都没有帮助我.一切都在本地工作,但在线它没有.

Khe*_*pri 36

我们能看到"Gebruiker"课吗?有错误似乎很简单.Gebruiker不被归为Serializable,因此无法将其置于Session for StateServer和SQLServer模式中.

编辑:

在查看您链接的代码之后,是的,这可能是因为该类不可序列化.LINQ生成的类应该是一个部分类,因此您可以实现自己的部分类,将其标记为Serializable,然后将满足sessionstate要求.

[Serializable()]
public partial class Gebruiker { //Lots of stuff }
Run Code Online (Sandbox Code Playgroud)

编辑2:

托马斯,你的Gebruiker类可能有一个现在看起来像这样的定义(或类似的东西)

namespace XYZ
{
   public partial class Gebruiker
}
Run Code Online (Sandbox Code Playgroud)

只需创建另一个具有相同命名空间的类文件,并使用Serializable属性定义该类

namespace XYZ
{
  [Serializable()]
  public partial class Gebruiker{}
}
Run Code Online (Sandbox Code Playgroud)

这就是你在这个文件中所需要的一切.这样,当创建LINQ生成的Gebruiker类时,不会覆盖serializable属性.


fae*_*ter 7

常规会话只是存储在内存中,但是当您在负载平衡环境中工作时,您不能依赖处理回发的同一服务器,为什么会话必须存储在主要是SQL Server和StateServer的共享会话机制中.

这可以在machine.config或比app web更深的地方配置.为什么可能不知道这样的改变,但它可以解释你在部署到互联网时的突然异常.

正如另一个答案和评论中所提到的,当会话对象不是[Serializable]时会出现问题; 当你在常规会话中工作时,对象不是序列化的,而是存储在服务器内存中,但是联机会引入不同的会话机制以及对象可序列化的需要.