Saturday, 25 September 2010

So what’s this IExtensibleDataObject interface all about then?

When defining WCF data contracts (i.e. classes marked with a DataContractAttribute) problems can occur with regards to versioning; adding new members to a data contract for example. Data could be passed to a version of a service that was not expecting it. This data could then be lost at the service.

By implementing the IExtensibleDataObject interface a data contract class can preserve unknown members included in the serialised type. The DataContractSerializer will populate the ExtensionDataObject dictionary required by the interface during the deserialisation process. The dictionary is then used to provide the additional member values upon serialisation.

This is useful:

  • When clients send additional unknown data to a service that should be returned intact
  • When a later version client sends data to an earlier version service and you want to preserve the unknown data for version tolerance

Ignoring IExtensibleDataObject at the service level

Even if data contract classes implement IExtensibleDataObject you can tell a service to ignore it by applying a service behaviour.

<behaviour name="serviceBehavior">
    <dataContractSerializer ignoreExtensionDataObject="true" />
</behaviour>

Additional information

The MSDN documentation states:

*The IExtensibleDataObject interface provides a single property that sets or returns a structure used to store data that is external to a data contract. The extra data is stored in an instance of the ExtensionDataObject class and accessed through the ExtensionData property. In a roundtrip operation where data is received, processed, and sent back, the extra data is sent back to the original sender intact. This is useful to store data received from future versions of the contract. If you do not implement the interface, any extra data is ignored and discarded during a roundtrip operation.” *

* IExtensibleDataObject Interface

Saturday, 25 September 2010