Development With A Dot

Blog on development in general, and specifically on .NET



My Friends

My Links

Permanent Posts

Portuguese Communities

Circular References in WCF

Updated: thanks to Nobody for the remark! 

Suppose you want to transmit a list of items where possibly some of them refer to other items on the list. For example:


public class MenuItem



    public String Text { get; set; }


    public String NavigationUrl { get; set; }


    public MenuItem Parent { get; set; }


If you want to preserve bandwidth, and avoid circular references - for example, one menu item having itself as its parent - you should use the IsReference property on the [DataContract] attribute:

[DataContract(IsReference = true)]

This property exists since .NET 3.5 SP1 (and .NET 3.0 SP1, for that matter), and it makes it unnecessary to write a specific behavior to set the preserveObjectReferences on the DataContractSerializer class (see Sowmy's post: A welcome addition!


nobody said:

Dont you mean the DataContract attribute?

# June 3, 2009 9:30 AM