The data contract is a description of the data structure used for the exchange, and is the basis of serialization and deserialization. In a WCF application, the client and server must be able to exchange data effectively through the equivalent data contract. Over time, inevitably, we will face changes in the version of the data contract, such as the addition and deletion of data members, member names or namespaces, and so on, how to avoid the changes in the data contract version of the client's existing programs, which is an important discussion in this section.
Equivalence of data contracts
Data contracts define the structure of data in a vendor-neutral, platform-independent form (XSD), and WCF through DataContractAttribute and DataMemberAttribute is designed to add some metadata to the corresponding type, Helps DataContractSerializer serialize objects of the appropriate type into XML with our desired structure. On the client side, WCF service invocation is not entirely dependent on a specific type, and it is best if the client has the exact same data contract type definition as the server. If the existing data contract type of the client has some differences with the published data contract, we can still make the data contract equivalent by DataContractAttribute and DataMemberAttribute these two properties.
In short, if the two different data contract type objects that host the same data can eventually serialize the same XML, then the two data contracts can be considered equivalent data contracts. The equivalent data contract has the same contract name, namespace, and data members, and the data members are required to appear in the same order. For example, the following two forms of data contract definitions, although their type and member names are different, even the corresponding members are defined in different order, but due to the rational use of DataContractAttribute and datamemberattribute these two characteristics Ensures that their objects are eventually serialized with the same XML structure, so they are two equivalent data contracts.
1: [DataContract (Namespace = "http://www.artech.com/")]
2:public class Customer
3: {
4: [DataMember (Order=1)]
5:public string FirstName
6: {get;set;}
7:
8: [DataMember (order = 2)]
9:public string LastName
{get; set;}
11:
: [DataMember (order = 3)]
13:public string Gender
: {get; set;}
15:}
1: [DataContract (Name = "Customer", Namespace = "http://www.artech.com/")]
2:public class Contact
3: {
4: [DataMember (Name = "LastName", order = 2)]
5:public string Surname
6: {get; set;}
7:
8: [DataMember (Name = "FirstName", order = 1)]
9:public string Name
{get; set;}
11:
[DataMember (Name = "Gender", order = 3)]
13:public string Sex
: {get; set;}
15:}
The main manifestation of the difference in the data contract version is the addition and deletion of data members. How to ensure that a new data member is added to the data contract, or that an existing data member is removed from the data contract, that the normal service invocation (for the service provider) of the existing client is guaranteed, or that the normal invocation of the existing service (for the service consumer) is ensured. This is the problem that the data Contract version control needs to solve.
Ii. additions to data members
Let's start with the question of adding data members, as shown in the following code, with the addition of a new data member on the server side based on the existing data contract (CUSTOMERV1): Address. However, the client still makes service calls through the data contract CustomerV1. Then, the client serializes the customer object according to the definition of CustomerV1, and the server deserializes the received XML as defined by the CustomerV2, and discovers that the address member is missing. So what is the serialization and deserialization behavior of DataContractSerializer in the absence of such a data member?
1: [DataContract (Name = "Customer", Namespace = "http://www.artech.com")]
2:public class CustomerV1
3: {
4: [DataMember]
5:public string Name
6: {get; set;}
7:
8: [DataMember]
9:public string Phoneno
{get; set;}
11:}
1: [DataContract (Name = "Customer", Namespace = "http://www.artech.com")]
2:public class CustomerV2
3: {
4: [DataMember]
5:public string Name
6: {get; set;}
7:
8: [DataMember]
9:public string Phoneno
{get; set;}
11:
: [DataMember]
13:public string Address
: {get; set;}
15:}