Google+

Monday, November 24, 2014

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'


In this article we see How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled', at the Server Side of our SPA application :
How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'




 How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'


The serialization error that we're taking care of here, is originated when a Data Model contains an object with a self-reference property , that means, it references an entity of the same type of the containing class, or the more usual case, when we have relationships between several objects, which are referenced by others in such a way that is created a circular reference.
For example, let's say that on our SPA's server side , we have the following data Model , in which a "Blog" has a reference to its "Blogger" , and this later owns in turn many "Blogs":

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 1

That means, if you open some Blog , and open its Blogger property, you will then see the Blogs that the Blogger owns, and , between them , the original Blog that you opened earlier, and that is a circular reference.
This can happen for instance, when you have a WCF service or a Web API which use eager loading:


How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 2

The corresponding MSDN article addressing this "contains cycles and cannot be serialized if reference tracking is disabled" error  takes on account only the former study case, in which an entity references itself: let's say, a "Person"  contains a property referencing some other "Person":

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 3

MSDN states that the solution is to append to the serialization DataContract Attribute the "IsReference" boolean value set to "true". But in most cases, you're prone to find the second XML-JSON serialization case, in which many objects are related by ONE-TO-MANY relationships between them, as the following:

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 4

To solve this problem,  make a list of the classes that are REFERENCED by other classes,  the entities which are at the " 0 .. 1 " side of the one-to-many relationships. In our example, there are just two classes that behave like that: the Blogger entity  and the Blog one, sonce the "Comment" class is not referenced anywhere.
Therefore, go to the classes files and append to the DataContract attribute the "IsReference" set to true, as follows:

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 5

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 6

Then the reference tracking will got enabled and the cycles will be fixed.
In addition, if you like, it's useful to define "Metadata classes" and set the Attributes there: something  like this:
How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 7



After re-running the SPA's Server Side REST service, the WCF or WebAPI will render the data avoiding cycles :

How to fix the Server Side Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 8


The snapshot shows that a "Blog" containing a "Blogger" property,  has a self reference and tries to reference itself, but it will be use instead the reference " z:Id=i1 " , avoiding a cycle. 
The same thing occurs for the "Comments" object, while trying to reference the containing "Blog".

Hoping it will help you...


By Carmel Shvartzman

עריכה: כרמל שוורצמן

No comments:

Post a Comment