2011-05-04 107 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」 例子:

相關問題