Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF error serializing cycle reference

I'm trying to return lists of objects that have references to another objects and viceversa.

I only want the lazy load to get the "first level children", I mean, if I have a "Person" object with a "Place" property, I want the place data to be loaded but not every object in "Place" object needs to be loaded... because this would ahead to a cyclic reference...

I've read that I could do this by using [DataContract(IsReference = true)] on every Object.

I've set every object in the model (auto-generated by EF) with that decoration but it's still failing when trying to send it back to the service caller.

Am I missing anything? Thanks in advance.

like image 371
zapico Avatar asked Dec 29 '25 02:12

zapico


1 Answers

I have used the [DataContract(IsReference=true)] successfully to solve the cyclic dependency issue in the past. Admittedly they were not objects generated by EF, but I' not sure how much that should matter.

What is the exact error?

Is it that the graph is getting to big?

Could it be that your objects are not the same instances, but different instances of the conceptually same type?

So when your TypeA-instance1 gets serialized and it has a reference to TypeB-instance1 which has a Reference to TypeA-instance1 the 2 actual TypeA-instance1 objects to not compare equal so the serializer does not attempt to reuse the references?

You could override the equals method on your objects and do some equality testing based on properties of your object rather than the default memory address that will be used.

like image 74
Sam Holder Avatar answered Dec 31 '25 14:12

Sam Holder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!