1

I am getting this error from time to time with serialization and I don't know which file I need to serialize. The pages are on and off one time they load other time they don't. Here is the error I am getting:

System.Web.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.Runtime.Serialization.SerializationException: Type 'System.Data.Entity.DynamicProxies.Product_CC06DC79809AC788823D74B88F94CCB7155ADCE697911AF73214FB8B5F2B2EB4' in Assembly 'EntityFrameworkDynamicProxies-Nop.BusinessLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) at System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) at System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) at System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) at System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) at System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) at System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

So basically what I think is something with the entity model but I want to know how I can serialize that. Thanks in advance, Laziale

Tom Studee
  • 10,316
  • 4
  • 38
  • 42
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • does this cover it? http://stackoverflow.com/questions/7276507/serializable-classes-and-dynamic-proxies-in-ef-how – Marc Gravell Jun 07 '12 at 20:42
  • Do you need to serialize the whole entity? It may be better to just store the unique ID in the session and retrieve the entity itself from the database. – Macros Jun 07 '12 at 20:43
  • 1
    btw, does `DataContractSerializer` work with your object? If so, another option would be: serialize with `DataContractSerializer` and just store the output – Marc Gravell Jun 07 '12 at 20:43
  • +1 for data contract serializer. – DarthVader Jun 07 '12 at 22:39
  • Why do you need to do that? This kind of errors are usually symptoms of something that could be done right in another way. – Ivo Jun 08 '12 at 04:07

1 Answers1

3

System.Data.Entity.DynamicProxies.Product (Error with this class)

Should add [DataContract] attribute on class and [DataMember] on fields like that

using System.Runtime.Serialization;
[DataContract]
public class Account
{
    [DataMember]
    public System.Guid ID { get; set; }
    [DataMember]
    public System.Guid AccountSubTypeID { get; set; }
    [DataMember]
    public Nullable<int> IndustryTypeID { get; set; }
}
Yael
  • 1,566
  • 3
  • 18
  • 25
Cu Hoang Kim
  • 103
  • 8