Cannot add the same member twice to a SerializationInfo object

Posts   
 
    
brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 21-Aug-2007 19:40:10   

LLBLGen Pro 2.0 ASP.NET C# 2.0 Adapter Model

DB: SQL Server 2005

Issue:

We just changed our session state to use SQLServer and now we are having Serialization issues on several entity (moving the app to a farm). As noted from other threads - we do not have any inherited entities.

When we use session state inproc the application works.

Error received:

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: Cannot add the same member twice to a SerializationInfo object.] System.Runtime.Serialization.SerializationInfo.AddValue(String name, Object value, Type type) +2672614 System.Runtime.Serialization.SerializationInfo.AddValue(String name, Object value) +57 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.GetObjectData(SerializationInfo info, StreamingContext context) +224 STEI.Cafe.Data.LLBL.EntityClasses.CafeEmbalmEntity.GetObjectData(SerializationInfo info, StreamingContext context) +150 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) +281 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) +50 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +411 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +489 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +131 System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1549

[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) +1638 System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +35 System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +626 System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +259 System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length) +61 System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +115 System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +612 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +138 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +65

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Aug-2007 11:02:39   

Could you please check that you don't have 2 properties of different types but having the same name in the CafeEmbalmEntity.

Example: a field called Customer (which stores the customerID), and the object Customer (which referes to the related CustomerEntity).

If this is the case then you may just rename one of them.

brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 22-Aug-2007 15:23:39   

Thanks for the fast response Walaa. I verified that I do not have duplicate names in the CafeEmbalmEntity.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Aug-2007 16:06:02   

Is this re-producable using another entity. i.e. don't store the CafeEmbalmEntity in the session and try storing another entity and check if the same exception occurs.

Also would you please post the runtime library version? (ref: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7725)

brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 22-Aug-2007 17:00:55   

I have several entities that are being stored in session that are not causing exceptions.

Runtime Version is v2.0.50727

Thanks again!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Aug-2007 17:18:29   

That's not the correct runtime library version please check the following thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7725

brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 22-Aug-2007 17:27:23   

Runtime version is 2.0.0.61107

Sorry about that.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Aug-2007 11:11:56   

That's somehow old. And there was a later fix that may affect this issue. Would you please try to use the latest version?

If this doesn't help, then I'll need you to post a repro project.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 23-Aug-2007 16:12:00   

Could you test for me the following: MemoryStream s = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(s, entitiesAddedToSession);

does that work? (it should). If so, the entities are serializable.

I find it a bit odd that you get this error, as the entities are serializable in any way: both xml and binary.

Frans Bouma | Lead developer LLBLGen Pro
brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 23-Aug-2007 16:52:38   

Otis, thanks for the help.

I tried the code you suggested and I get the same error

Cannot add the same member twice to a SerializationInfo object.

Line 37: MemoryStream s = new MemoryStream(); Line 38: BinaryFormatter formatter = new BinaryFormatter(); Line 39: formatter.Serialize(s, new CafeEmbalmEntity()); Line 40: Line 41: AjaxPro.Utility.RegisterTypeForAjax(typeof(ContractSummary));

Source File: c:\Projects\STEI\Cafe\CafeSolution\STEI.Cafe.Web.UI\Contract\ContractSummary.aspx.cs Line: 39

[SerializationException: Cannot add the same member twice to a SerializationInfo object.] System.Runtime.Serialization.SerializationInfo.AddValue(String name, Object value, Type type) +2672614 System.Runtime.Serialization.SerializationInfo.AddValue(String name, Object value) +57 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.GetObjectData(SerializationInfo info, StreamingContext context) +224 STEI.Cafe.Data.LLBL.EntityClasses.CafeEmbalmEntity.GetObjectData(SerializationInfo info, StreamingContext context) +150 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) +281 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) +50 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +438 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +131 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) +12 ContractSummary.OnInit(EventArgs e) in c:\Projects\STEI\Cafe\CafeSolution\STEI.Cafe.Web.UI\Contract\ContractSummary.aspx.cs:39 System.Web.UI.Control.InitRecursive(Control namingContainer) +344 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1792

brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 23-Aug-2007 17:22:19   

looking at this some more, I don't think it can be a binary serialization problem. Some entities are working fine and others are not. the following code works.

    MemoryStream s = new MemoryStream();
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(s, new CafeContractEntity());

So this leads me to believe there is something wrong with a few of my entities. I have compared working entities with non-working entities and I can't see where they are wrong.

Thanks again for the help!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 23-Aug-2007 18:00:18   

This is the code in EntityBase2.GetObjectData():


info.AddValue("_fieldsData", ((EntityFields2)_fields).GetFieldsDataArray());
info.AddValue("_fieldsFlags", ((EntityFields2)_fields).GetFieldsTrackingFlagsArray());
info.AddValue("_fieldsState", _fields.State);
info.AddValue("_fieldsIsDirty", _fields.IsDirty);
info.AddValue("_name", _name);
info.AddValue("_isNew", _isNew);
info.AddValue("_validator", _validator);
info.AddValue("_objectID", _objectID);
info.AddValue("_relatedEntitySyncInfos", _relatedEntitySyncInfos, typeof(FastDictionary<Guid, FastDictionary<string, EntitySyncInfo<IEntity2>>>));
info.AddValue("_field2RelatedEntity", _field2RelatedEntity, typeof(FastDictionary<string, Guid>));
info.AddValue("_concurrencyPredicateFactoryToUse", _concurrencyPredicateFactoryToUse);
info.AddValue( "_dataErrorInfoError", _dataErrorInfoError );
info.AddValue( "_dataErrorInfoErrorsPerField", _dataErrorInfoErrorsPerField );

Could you post the CafeEmbalmEntity.GetObjectData method (which is generated) ?

Also, be sure to use a later build of the runtime. thanks.

Frans Bouma | Lead developer LLBLGen Pro
brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 24-Aug-2007 15:41:20   

I am now using Runtime 2.0.7.810. Having the same issues.

        /// <summary>ISerializable member. Does custom serialization so event handlers do not get serialized. Serializes members of this entity class and uses the base class' implementation to serialize the rest.</summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {


            info.AddValue("_cafeAdditionalRemovalPersonEmployee", _cafeAdditionalRemovalPersonEmployee);
            info.AddValue("_cafePermissionEmployee", _cafePermissionEmployee);
            info.AddValue("_cafeEmbalmerEmployee", _cafeEmbalmerEmployee);
            info.AddValue("_name", _name);
            info.AddValue("_cafeRemovalPersonEmployee", _cafeRemovalPersonEmployee);
            info.AddValue("_relationship", _relationship);
            info.AddValue("_cafeCase", _cafeCase);
            
            // __LLBLGENPRO_USER_CODE_REGION_START GetObjectInfo
            // __LLBLGENPRO_USER_CODE_REGION_END
            base.GetObjectData(info, context);
        }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Aug-2007 15:58:45   

**info.AddValue("_name", _name);** That's added twice. One in the EntityBase GetObjectData(), and one in your EntityClass GetObjectData().

brianDDS
User
Posts: 8
Joined: 21-Aug-2007
# Posted on: 24-Aug-2007 18:12:32   

Ah, that was it. Thanks for your help!