Reference vs Value in WCF

8/20/2011 1:23:02 PM

WCF turns referenced objects into value data by default. This is a behavior that can cause trouble and decrease performance. Learn how to define reference or value style navigation for your serialized data.

In C#, in memory object structures are navigated by reference. If you transport these object structures to a client over WCF they are serialized, transported over the wire, and then reconstituted into the WCF client's memory via deserialization. All this is fine and often a smooth process, however, Stay alert! Cause the default WCF behavior of serializing the object graph is to turn it into a By Value data and create new xml data nodes for each reference to the same (original in memory) instances.

Ie (very simplified):
Allan Scott -> Sweden
John Johnsson -> Sweden
Martin Neheymer -> Germany
instead of the reference style:
Allan Scott -> 1
John Johnsson -> 1
Martin Neheymer -> 2

This can have a huge impact for the amount of data transported over the network as well as for the serialization -> deserialization process CPU utilization and memory consumption. Ofcourse the impact depends of the data and the object structure.


It is, since .net 3.5 sp1, possible to define a class to be a reference type instead using [Datacontract(IsReference=true)] attribute on your DTO.

Be aware of this phenomena and know there is a way to make an active choice on how WCF should handle references in your object structures.

Here's a nice post on the subject.