WCF – Data Contract version round-tripping using IExtensibleDataObject

With WCF, provided that you’re using the DataContractSerializer and not the NetDataContractSerializer, you have a certain degree of protection against data contract changes from the client’s perspective.

Existing contracts will still work if existing members are not removed from the data contract, which means you are free to add new members to the data contract on the server side without affecting existing clients. This gives you some backward compatibility and allows you to incrementally evolve your data contracts in a non-breaking way.

Round-Tripping

Round-tripping occurs when data passes from a new version to an old version and back to the new version of a data contract. Round-tripping guarantees that no data is lost. Enabling round-tripping makes the type forward-compatible with any future changes supported by the data contract versioning model.

To enable round-tripping your type must implement the IExtensibleDataContract interface and add the ExtensionData property of ExtensionDataObject type (see the MSDN article in the References section for an example).

It’s worth noting that the data stored in the ExtensionData property is not public retrievable and is only used by the WCF infrastructure.

And finally, to turn it off:

The round-tripping feature may be turned off, either by setting ignoreExtensionDataObject to true in the DataContractSerializer constructor or by setting theIgnoreExtensionDataObject property to true on the ServiceBehaviorAttribute. When this feature is off, the deserializer will not populate the ExtensionData property, and the serializer will not emit the contents of the property.

References:

MSDN article on Forward-Compatible Data Contracts

MSDN article on Best Practices on WCF Data Contract Versioning

MSDN – WCF Guildelines and Best Practices