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!


1 Comment

Comments have been disabled for this content.