gpt4 book ai didi

c# - 无法序列化 session 状态。第2部分

转载 作者:行者123 更新时间:2023-11-30 15:44:42 26 4
gpt4 key购买 nike

在解决了昨天的 ( Unable to serialize the session state) 错误之后,我现在遇到了这个错误(在同一页面)。

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.

来源错误:

An unhandled exception was generated during the execution of thecurrent web request. Information regarding the origin and location ofthe exception can be identified using the exception stack trace below.

堆栈跟踪:

[SerializationException: Type'System.Data.Linq.ChangeTracker+StandardChangeTracker' in Assembly'System.Data.Linq, Version=4.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089' is not marked as serializable.]System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeTypetype) +9452985System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Typetype, StreamingContext context) +247System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()+160 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Objectobj, ISurrogateSelector surrogateSelector, StreamingContext context,SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,ObjectWriter objectWriter, SerializationBinder binder) +218System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfoobjectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +388System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Objectgraph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)+444 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(StreamserializationStream, 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 sessionstate objects, and as a result non-serializable objects orMarshalByRef objects are not permitted. The same restriction appliesif similar serialization is done by the custom session state store in'Custom' mode.]System.Web.Util.AltSerialization.WriteValueToStream(Object value,BinaryWriter writer) +1793System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Objectvalue, BinaryWriter writer) +34System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriterwriter) +638System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreDataitem, Stream stream) +244System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreDataitem, Int32 initialStreamSize, Byte[]& buf, Int32& length, BooleancompressionEnabled) +67System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContextcontext, String id, SessionStateStoreData item, Object lockId, BooleannewItem) +114System.Web.SessionState.SessionStateModule.OnReleaseState(Objectsource, EventArgs eventArgs) +807System.Web.SessionState.SessionStateModule.OnEndRequest(Object source,EventArgs eventArgs) +184System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+148 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

我现在在我的 LINQ 数据库文件中放置了一个可序列化类型。

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Gebruiker")]
[Serializable()]
public partial class Gebruiker { }

public partial class Gebruiker : INotifyPropertyChanging, INotifyPropertyChanged
{

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private string _GebruikerID;

private string _GebruikerVoornaam;

private string _GebruikerNaam;

private string _GebruikerEmail;

private string _GebruikerWachtwoord;

private string _GebruikerPermissies;

//...
}

最佳答案

当使用 BinaryFormatter(包括这个 session 状态提供程序)时,图中的所有类型都必须可以通过 [Serializable]ISerializable。这里最常见的“陷阱”是事件。通常你可以添加:

[field:NonSerialized]

任何类似字段的事件声明,即

[field:NonSerialized]
public event EventHandler FooChanged;

但显然这在生成的代码中并不方便。

IMO,这里真正的问题是在状态中使用丰富的对象。 session 状态和缓存对象实际上应该是基本版本——具有最小麻烦和复杂性的基本 DTO——只是数据(我认为完全不变性或冰棒不变性也是不错的选择).我会重构为要存储的内容添加一个 DTO,然后只存储它。

另一种方法是在交换序列化程序的过程中更改状态实现。在 DBML 设计器中,您可以使 LINQ 模型“单向”,这意味着 DataContractSerializer 将与它们一起工作;所以 DataContractSerializer 可能会进行合理的换出;但是编写提供程序是艰苦的工作,而且与您的业务需求无关。我只想写 DTO ;p

最后一个选择是在您的部分类中实现ISerializable,并且只存储/获取您实际需要的值。

(除了事件之外,导致对象不能被 BinaryFormatter 序列化的任何其他字段也会失败)

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com