Today I ran into the issue sending object graphs that contain reference cycles through WCF. I then got the following exception:
Object graph for type contains cycles and cannot be serialized if reference tracking is disabled
The default WCF serializer can't serialize object graphs with cycles. After some research on how to solve the issue, I found the following blog post:
http://zamd.net/2008/05/20/datacontract-serializer-and-isreference-property/
Basically, In .net Framework 3.5 SP1, DataContractSerializer supports by-ref object graph serialization by using the standard xsd:ID/xsd:IDREF attributes.
You can set the IsReference=true on your DataContract definition and serializer will generate XML elements with IDs/IDREFs attributes and will link them together rather embedding them inside each other(default behavior).
Also if you examine the XSD generated by WCF as part of the metadata export, it will also contain the standard ID/IDREF xml schema attributes. Because of this, xml can be correctly parsed and understood by any framework in a standard way.
This change will enable serialization of object graphs having circular references (which wasn’t possible previously – at least not without writing custom code) and will also reduce the size of the serialized xml.
That solved my issue, Great thing to know!
This is great! Works great! I've got other issues besides this, but this was just what I was looking for. Thanks!
Posted by: Michael | 09/06/2012 at 00:06