2011-05-04 117 views
24

把我的应用程序联机,并试图登录我什么时候得到这个错误:无法序列化会话状态

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 

我与会话那里做的唯一事情是这样的:

  • 会话[ “Rechten”] =“GlobaalAdmin”; (实施例)
  • 会话[ “gebruikerid”] = txtID.Text; (该ID在登录同)
  • 并重定向到另一个页面:Response.Redirect的( “LoggedIn.aspx”,真正的);
  • http://pastebin.com/jZprwA8m(把gebruiker到一个会话)

已经有关于这个问题的多个主题,但他们都不来帮助我。 一切工作在本地,但它不在网上。

+0

它看起来像一个Gebruiker对象的实例被沿途某处序列化(和它可能没有标注了[可序列化]属性)。 – 48klocs 2011-05-04 20:23:20

+3

原因在您发布的堆栈跟踪中明确声明:'[SerializationException:在Assembly'App_Code.qzuhycmn,版本= 0.0.0.0,Culture = neutral,PublicKeyToken = null'中键入'Gebruiker'未标记为可序列化。]' 。你真的确定你只是在会话中放置'string's,就像你说的那样? – 2011-05-04 20:24:20

+0

http://pastebin.com/jZprwA8m 是我唯一一次在gebruiker的“对象”上做出的。 – Schoof 2011-05-04 20:30:07

回答

29

,我们可以看到 “Gebruiker” 类?有错误似乎足够简单。 Gebruiker不能被认为是可序列化的,因此它不能放置在StateServer和SQLServer模式的Session中。

编辑:

看你的链接代码后,是的,这可能是因为类是不可序列化。 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属性不会被覆盖。

+0

“Gebruiker”类是使用LINQ自动生成的。 http://pastebin.com/nww2enQ5 – Schoof 2011-05-04 20:41:51

+0

@Thomas:这是如何阻止你向我们展示? – 2011-05-04 20:46:28

+0

@John:我发布了一个链接到类中的pastebin。 – Schoof 2011-05-04 20:51:58

6

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

这可以在machine.config中或其他地方比应用程序的web.config部署到互联网时,为什么可能不会意识到这种变化的,但它可以解释你突然异常更深的配置。

作为另一个答案和评价的问题提到然后就会出现在会话对象不是[序列化];而你是在常会的工作对象不是序列化只是存储在服务器内存中,但网上会引入不同的会话机制和必要的对象是可序列化。

+0

我忘了补充说我把当前“Gebruiker”的一个对象放入了一个会话中。 pastebin.com/jZprwA8m 我猜这是问题所在? – Schoof 2011-05-04 20:47:14

+0

好的,但你仍然应该能够将它标记为Serializable,因为它将作为一个部分类生成(据我记忆),所以在其他文件中,你可以把 [Serializable] 公共部分类Gebruiker {} – faester 2011-05-04 20:48:24

1

根据您发布的代码: 该类作为partial产生的,因此,所有你需要做的就是添加这样的另一部分定义(在相同的命名空间中产生的部分类,当然):

[Serializable] 
public partial class Gebruiker {} 
0

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

0

在我的情况下,我试图序列化非序列化的对象是HttpResponse。因此,如果提供的解决方案不起作用,您可以通过solution of unable to serialize session state文章。

希望它能帮助别人!

0

有些时候,你必须检查下面财产在你的web.config

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

如果SessionState的模式= “SQLServer的”,那么你必须使用 [序列化()] 公共部分类Gebruiker。

否则,你必须改变你的SessionState的模式为“INPROC” 例子:

相关问题