无法序列化会话状态
本文关键字:会话状态 序列化 | 更新日期: 2023-09-27 17:54:38
当将我的应用程序放在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
我对Sessions做的唯一一件事是:
- Session["Rechten"] = "GlobaalAdmin";(例子)
- Session["gebruikerid"] = txtID.Text;(他们登录的ID)
- 将它们重定向到另一个页面:Response.Redirect("LoggedIn.aspx",true);
- http://pastebin.com/jZprwA8m(将gebruiker放入会话)
我们能看到"Gebruiker"类吗?这个错误看起来很简单。Gebruiker没有被属性为可序列化的,因此它不能在staterver和SQLServer模式下被放置在Session中。
编辑:查看您链接的代码后,是的,这可能是因为类不可序列化。LINQ生成的类应该是一个分部类,这样你就可以实现你自己的分部类,把它标记为Serializable,然后满足会话状态的要求。
[Serializable()]
public partial class Gebruiker { //Lots of stuff }
编辑2:Thomas,你的Gebruiker类现在可能有一个像这样的定义(或类似的东西)
namespace XYZ
{
public partial class Gebruiker
}
创建另一个具有相同命名空间的类文件,并使用Serializable属性定义该类
namespace XYZ
{
[Serializable()]
public partial class Gebruiker{}
}
这就是这个文件中需要的所有内容。这样,当LINQ生成的Gebruiker类被创建时,serializable属性就不会被覆盖。
常规会话只是存储在内存中,但是当你在一个负载均衡的环境中工作时,你不能依赖于同一台服务器处理回发,为什么会话必须存储在一个共享的会话机制中,主要是SQL server和StateServer。
可以在machine中配置。配置或比应用程序web更深入的地方。配置为什么可能不知道这样的变化,但它可以解释你的突然异常部署到互联网。
正如在另一个答案和评论中提到的,当会话对象不是[Serializable]时,问题就会出现;当您在常规会话中工作时,对象不是序列化的,而只是存储在服务器内存中,但是联机时会引入不同的会话机制,并且需要对象是可序列化的。
根据你发布的代码:这个类是作为partial
生成的,所以您所需要做的就是添加另一个像这样的部分定义(当然是在与生成的部分类相同的名称空间中):
[Serializable]
public partial class Gebruiker {}
还有另一种解决方案,在继承不可序列化类的同时也实现可序列化接口GetObjectData方法。继承的类也应该标记为[Serializable]。
有时你必须检查下面的属性在你的web.config
<sessionState mode="InProc" ........../>
如果你在SharePoint 2013上遇到这个错误信息,不要忘记运行下面的PowerShell cmd命令:
Enable-SPSessionStateService -DefaultProvision
此错误信息是SP2013中新增的;在SP2010中查看这个问题:会话状态只能在enableSessionState在配置
在我的例子中,我试图序列化非序列化对象HttpResponse
。因此,如果提供的解决方案不起作用,您可以通过无法序列化会话状态文章的解决方案。
希望对别人有所帮助!
如果SessionState mode =" SQLServer"则必须使用(序列化())public partial class Gebruiker .
否则你必须改变你的SessionState模式为"Inproc"例子: