无法序列化会话状态

当我的应用程序联机并尝试登录时,我收到此错误:

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 

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

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

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

我们能看到“Gebruiker”课吗? 有错误似乎很简单。 Gebruiker不归因于Serializable,因此它不能放在Session for StateServer和SQLServer模式中。

编辑:

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

 [Serializable()] public partial class Gebruiker { //Lots of stuff } 

编辑2:

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

 namespace XYZ { public partial class Gebruiker } 

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

 namespace XYZ { [Serializable()] public partial class Gebruiker{} } 

这就是你在这个文件中需要的全部内容。 这样,当创建LINQ生成的Gebruiker类时,不会覆盖serializable属性。

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

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

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

基于您发布的代码:该类是partial生成的,因此您需要做的就是添加另一个部分定义(当然,与生成的部分类在同一名称空间中):

 [Serializable] public partial class Gebruiker {} 

还有另一种解决方案,虽然inheritance了非可序列化的类,但也包含了ISerlializable接口的GetObjectData方法。 inheritance的类也应标记为[Serializable]。

如果您在SharePoint 2013上遇到此错误消息,请不要忘记运行以下PowerShell cmdlet:

 Enable-SPSessionStateService -DefaultProvision 

此错误消息是SP2013中的新消息; 请参阅SP2010的此问题: 只有在配置中将enableSessionState设置为true时才能使用会话状态

在我的例子中,我试图序列化HttpResponse的非序列化对象。 因此,如果提供的解决方案不起作用,您可以通过无法序列化会话状态文章的解决方案 。

希望对别人有所帮助!

有时您必须在web.config中检查以下属性

  

如果SessionState mode =“SQLServer”,那么你必须使用[Serializable()]公共部分类Gebruiker。

否则你必须将SessionState模式更改为“Inproc”示例: