Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

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:

[DataContract]

public class MenuItem

{

    [DataMember]

    public String Text { get; set; }

    [DataMember]

    public String NavigationUrl { get; set; }

    [DataMember]

    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: http://blogs.msdn.com/sowmy/archive/2006/03/26/561188.aspx). A welcome addition!

Comments

nobody said:

Dont you mean the DataContract attribute?

# June 3, 2009 9:30 AM