无法序列化会话状态

时间:2011-05-04 20:07:25

标签: 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 

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

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

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

8 个答案:

答案 0 :(得分:31)

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

编辑:

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

[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属性。

答案 1 :(得分:6)

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

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

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

答案 2 :(得分:1)

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

[Serializable]
public partial class Gebruiker {}

答案 3 :(得分:0)

还有另一种解决方案,虽然继承非可序列化类也实现了Serializable接口GetObjectData方法。 继承的类也应标记为[Serializable]。

答案 4 :(得分:0)

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

Enable-SPSessionStateService -DefaultProvision

此错误消息是SP2013中的新消息;请参阅SP2010的这个问题:Session state can only be used when enableSessionState is set to true either in a configuration

答案 5 :(得分:0)

就我而言,我试图序列化HttpResponse的非序列化对象。因此,如果提供的解决方案不起作用,您可以查看solution of unable to serialize session state文章。

希望对别人有所帮助!

答案 6 :(得分:0)

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

<sessionState mode="InProc" ........../>

答案 7 :(得分:0)

如果SessionState模式=&#34; SQLServer&#34;然后你必须使用 [序列化()] 公共部分类Gebruiker。

否则你必须将SessionState模式更改为&#34; Inproc&#34; 示例: