nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

NHibernate bags are empty after wcf serialization

Open Cyril035 opened this issue 3 years ago • 8 comments

Hi there,

I used nhibernate v4.1.4 for a long time and all works perfectly until we decide to update to lastest version. (5.3.10) We use it on a wcf service. Data loaded correctly but when objects are serialiazed (with wcf mechanisms) all collections are gone away... :( I precise that all collections load correctly with data inside before serialization.

I try to debug with this snippet

var myObjectWithCollections = Session.Query<MyClass>().Single(x => x.Id == id);
var serializer = new NetDataContractSerializer();
var wcfMessage = new FileStream("soapMsg.xml", FileMode.Create);
serializer.WriteObject(wcfMessage , myObjectWithCollections);

When serializer.WriteObjectis called, all collection are "flushed" :(

I decide for testing to change the type of one collection from IList<T> to ICollection<T> to test and....tadaaaaaa. It works. Unfortunately, I don't know why and I don't want to change all collection type to this ICollection

After serialization, normally collections are of type PersisentGenericBag which implement ICollection<T> AND IList<T> so it sould work ! I miss something but what ?

NOTE : all my collections are "bags" in my mappings files (hbm.xml). The loading mode was neither eager nor lazy. With 4.1.4 mappings were configured with full loading.

Have you got some ideas cauz I'm a bit confused now :(

Subject posted to stackoverflow too

Cyril035 avatar Jan 04 '22 12:01 Cyril035

To clarify, when you say "flushed", do you mean that the collections appear empty on the client side? Because usually when talking about NHibernate, "flushing" is about transmitting changes back to the database.

Do they appear empty before server side before serialisation?

On Tue, 4 Jan 2022, 13:38 Cyril035, @.***> wrote:

Hi there,

I used nhibernate v4.1.4 for a long time and all works perfectly until we decide to update to lastest version. (5.3.10) We use it on a wcf service. Data loaded correctly but when objects are serialiazed (with wcf mechanisms) all collections are gone away... :( I precise that all collections load correctly with data inside before serialization.

I try to debug with this snippet

var myObjectWithCollections = Session.Query<MyClass>().Single(x => x.Id == id); var serializer = new NetDataContractSerializer(); var wcfMessage = new FileStream("soapMsg.xml", FileMode.Create); serializer.WriteObject(wcfMessage , myObjectWithCollections);

When serializer.WriteObjectis called, all collection are "flushed" :(

I decide for testing to change the type of one collection from IList to ICollection to test and....tadaaaaaa. It works. Unfortunately, I don't know why and I don't want to change all collection type to this ICollection

After serialization, normally collections are of type PersisentGenericBag which implement ICollection AND IList so it sould work ! I miss something but what ?

NOTE : all my collections are "bags" in my mappings files (hbm.xml). The loading mode was neither eager nor lazy. With 4.1.4 mappings were configured with full loading.

Have you got some ideas cauz I'm a bit confused now :(

Subject posted to stackoverflow https://stackoverflow.com/questions/70569750/nhibernate-bags-are-flushed-during-wcf-serialization too

— Reply to this email directly, view it on GitHub https://github.com/nhibernate/nhibernate-core/issues/2972, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADVWL6Z6SH7IW63QOIJXOTUULS4VANCNFSM5LHLI4UQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

oskarb avatar Jan 04 '22 19:01 oskarb

Yeah, sorry for this. I mean collections are filled with data from database and after serialization they are empty

Cyril035 avatar Jan 04 '22 19:01 Cyril035

Just a guess... Do you have proxyfactory.factory_class session factory setting set? If yes - try to remove it. If not - try to set it to NHibernate.Bytecode.DefaultProxyFactoryFactory (which is despite the name is legacy proxy factory)

If it makes no difference - find last version where it works (5.2, 5.1 or 5.0)

bahusoid avatar Jan 05 '22 12:01 bahusoid

I thought about this test, but actually entities are not lazy so no proxy need to be used if I'm right. Anyway I did the test and the pb still there :( I go back to the last working version (4.1.1) and lol... don't work too... Something broken during update. I had to fix the breaking changes. I upgrade from NET4.6.1 to NET48 but same pb.

However, I still have this question in my head... Why it works with ICollection<T> type....??? What is the thing that can do the difference...?

Cyril035 avatar Jan 05 '22 14:01 Cyril035

Does serialization work properly with manually created and populated list? Something like:

var newObj =  new MyClass();
newObjc.Collection = new List<Child>();
newObj.Collection.Add(new Child());
serializer.WriteObject(wcfMessage ,newObj);

bahusoid avatar Jan 05 '22 15:01 bahusoid

@bahusoid Yeah, it works. Actually, in session context, NHibernate create a PersisantGenericBag for this collection which is normally serialized. The client should know this type. The wcf fail to serialize this PersistantGenericBag and retrieve a empty generic list instead.

Cyril035 avatar Jan 06 '22 11:01 Cyril035

I think it should be closed as external issue because of:

I go back to the last working version (4.1.1) and lol... don't work too... Something broken during update.

Or be converted to discussion.

bahusoid avatar Jan 12 '22 01:01 bahusoid

Hi there, @bahusoid, even something is going wrong to my project, I don't think it's only the problem. Always this question... why when I change from IList<T> to ICollection<T>, it works perfectly... ?

Cyril035 avatar Jan 15 '22 16:01 Cyril035