.NET Zone is brought to you in partnership with:

A software engineer from Italy, Stefano Ricciardi has started his career developing mobile applications for Motorola, and has now moved to enterprise applications development. He's currently busy developing libraries on .NET manipulating large amounts of data, mostly related to time series observations and their statistical analysis. In his spare time, he loves running and photography. Stefano is a DZone MVB and is not an employee of DZone and has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

Making a WCF Serializer Work with Circular References

07.02.2012
| 2061 views |
  • submit to reddit

The Problem of Circular References

Recently I had to model a tree-like structure using a variation of the GoF composite design pattern and to pass this class to a WCF service for further processing.

The class has circular reference as described by the following drawing:

Therefore off I went to naively create my DataContract along the following lines:

[DataContract(Namespace = "http://schemas.acme.it/2009/10"]
public class Node
{
	[DataMember(Name="Children", Order=2)]
    private IList<Node> children;

    [DataMember(Name="Id", Order=0)]
    public string Id { get; set; }

    [DataMember(Name="Parent", Order=1)]
    public Node Parent { get; set; }

    public Node()
    {
        children = new List<Node>()
    }

    public void AddChild(Node node)
    {
        this.children.Add(node);
        node.Parent = this;
    }

    public IList<Node> Children
    {
        get
        {
            IList<Node> result = new List(this.children);
            foreach (Node child in this.children)
            {
                foreach (Node innerChild in child.Children)
                {
                result.Add(innerChild);
                }
            }

            return result;
        }
    }
}

Unfortunately at run time I was greeted by the following exception:
Object graph for type 'System.Collections.Generic.List`1[[Node ...]]' contains cycles and cannot be serialized if reference tracking is disabled..

The Solution

Reading around I realized that up until some time ago you had to implement your own DataContractSerializer (see for example this post by Sowmy Srinivasan).

Luckily, it turns out that with NET 3.5 SP1 everything is much simpler: you just have to use the IsReference parameter in the DataContract attribute, as follows:

[DataContract(Namespace = "http://schemas.acme.it/2009/10", IsReference=true)]
public class Node
{
    // everything same as in the example above.
}

The Catch

For some reason, if you turn on the IsReference attribute, then you cannot set the IsRequired attribute to true on any DataMember of the DataContract. The reason is somehow explained here.

Published at DZone with permission of Stefano Ricciardi, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)