WCF — Data Contract version round-tripping using IExtensibleDataObject

With WCF, pro­vid­ed that you’re using the Dat­a­Con­tract­Se­ri­al­iz­er and not the Net­Dat­a­Con­tract­Se­ri­al­iz­er, you have a cer­tain degree of pro­tec­tion against data con­tract changes from the client’s per­spec­tive.

Exist­ing con­tracts will still work if exist­ing mem­bers are not removed from the data con­tract, which means you are free to add new mem­bers to the data con­tract on the serv­er side with­out affect­ing exist­ing clients. This gives you some back­ward com­pat­i­bil­i­ty and allows you to incre­men­tal­ly evolve your data con­tracts in a non-break­ing way.

Round-Tripping

Round-trip­ping occurs when data pass­es from a new ver­sion to an old ver­sion and back to the new ver­sion of a data con­tract. Round-trip­ping guar­an­tees that no data is lost. Enabling round-trip­ping makes the type for­ward-com­pat­i­ble with any future changes sup­port­ed by the data con­tract ver­sion­ing mod­el.

To enable round-trip­ping your type must imple­ment the IEx­ten­si­ble­Dat­a­Con­tract inter­face and add the Exten­sion­Da­ta prop­er­ty of Exten­sion­DataOb­ject type (see the MSDN arti­cle in the Ref­er­ences sec­tion for an exam­ple).

It’s worth not­ing that the data stored in the Exten­sion­Da­ta prop­er­ty is not pub­lic retriev­able and is only used by the WCF infra­struc­ture.

And final­ly, to turn it off:

The round-trip­ping fea­ture may be turned off, either by set­ting ignore­Ex­ten­sion­DataOb­ject to true in the Dat­a­Con­tract­Se­ri­al­iz­er con­struc­tor or by set­ting theIgnore­Ex­ten­sion­DataOb­ject prop­er­ty to true on the Ser­vice­Be­hav­iorAt­tribute. When this fea­ture is off, the dese­ri­al­iz­er will not pop­u­late the Exten­sion­Da­ta prop­er­ty, and the seri­al­iz­er will not emit the con­tents of the prop­er­ty.

References:

MSDN arti­cle on For­ward-Com­pat­i­ble Data Con­tracts

MSDN arti­cle on Best Prac­tices on WCF Data Con­tract Ver­sion­ing

MSDN – WCF Guilde­lines and Best Prac­tices