c# - 无法序列化 session 状态

标签 c# asp.net session

将我的应用程序放在网络服务器上并尝试“登录”时,出现以下错误:

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"] = "GlobalAdmin"; (例子)
  • Session["gebruikerid"] = txtID.Text; (他们登录时使用的 ID)
  • 并将它们重定向到另一个页面:Response.Redirect("LoggedIn.aspx",true);
  • http://pastebin.com/jZprwA8m (将 gebruiker 放入 session 中)

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

最佳答案

我们能看到“Gebruiker”级吗?错误似乎很简单。 Gebruiker 未被归类为可序列化,因此不能将其放置在 StateServer 和 SQLServer 模式的 Session 中。

编辑:

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

[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 类时,不会覆盖可序列化属性。

关于c# - 无法序列化 session 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889240/

相关文章:

c# - 错误 CS0246 : The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)

c# - LINQ to SQL - 确定我是否有过时数据

c# - Visual Studio 停止运行我的程序

c# - 如何动态地将列添加到 RadGrid?

c# - Amazon SES 电子邮件地址未验证

php - 从表中获取 session_user 排名

c# - 加密/ token 授权指南

session - ColdFusion session 问题 - 一个代理 IP 后面的多个用户 - cftoken 和 cfid 似乎是共享的

php - 设置 cookie 并插入数据库时​​遇到问题

c# - 传递参数 ASP.Net