Data Contract Equivalence

Two data contracts are considered equivalent if they have the same wire representation—that is, if they have the same schema. This can be the case if they define the same type (but not necessarily the same version of the type) or if the two data contracts refer to two different types with the same data contract and data member names. Equivalent data contracts are interchangeable: WCF will let any service that was defined with one data contract operate with an equivalent data contract.

The most common way of defining an equivalent data contract is to use the DataContract and DataMember attributes’ Name properties to map one data contract to another. In the case of the DataContract attribute, the Name property defaults to the type’s name, so these two definitions are identical:

[DataContract]
struct Contact
{...}

[DataContract(Name = "Contact")]
struct Contact
{...}

In fact, the full name of the data contract always includes its namespace as well, but as you have seen, you can assign a different namespace.

In the case of the DataMember attribute, the Name property defaults to the member name, so these two definitions are identical:

[DataMember]
string FirstName;

[DataMember(Name = "FirstName")]
string FirstName;

By assigning different names to the data contract and data members, you can generate an equivalent data contract from a different type. For example, these two data contracts are equivalent:

[DataContract] struct Contact { [DataMember] public string FirstName; ...

Get Programming WCF Services, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.