Eighth Lecture: Data Contract version control

Source: Internet
Author: User

Code

Https://yunpan.cn/cPns5DkGnRGNs Password: 3913

A data contract is a description of the data structure used for the interchange, and is the basis for serialization and deserialization.

In a WCF application, the client and the server must exchange data efficiently through an 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.

The most important manifestation of the difference between data contract versions 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 deleted from the data contract, the normal service invocation (for the service provider) of the existing client, or the normal invocation of the existing service (for the service consumer), is the problem that the data contract versioning is addressing.

For example: This scenario describes

1     /// <summary>2     ///version V13     /// </summary>4 5 [DataContract]6      Public classCustomerV17     {8 [DataMember]9          Public stringName {Get;Set; }Ten [DataMember] One          Public stringPhoneno {Get;Set; } A [DataMember] -          Public stringAddress {Get;Set; } -     } the  -     /// <summary> -     ///version V2 -     /// </summary> + [DataContract] -      Public classCustomerV2 +     { A [DataMember] at          Public stringName {Get;Set; } - [DataMember] -          Public stringPhoneno {Get;Set; } -  -}

The removal of data contract members results in the loss of data during the send-back process. The client makes the service invocation based on the data contract CustomerV1, and the implementation of the service is based on CustomerV2, then the XML generated by the serialized CustomerV1 object is transmitted to the server by the message, and the server is deserialized according to CustomerV2. There is no doubt that the address data will be lost. If the customer's information needs to be returned to the client, and the service needs to serialize the CustomerV2 object, the serialized generated XML certainly does not exist with the address data member. When the reply message is returned to the client, the client generates the CustomerV1 object according to CustomerV1, and the Address property that originally assigned the value is now null.

For the client, this is a strange and unacceptable thing: "Why is the data sent-after the return will be lost for no reason"?

In order to solve this problem

WCF defines a special interface, System.Runtime.Serialization.IExtensibleDataObject, that defines only one Extensiondataobject type attribute member. For a data contract that implements IExtensibleDataObject, WCF serializes the value of the ExtensionData property to XML when it is serialized, and if the XML is found to contain data that is not in the data contract during deserialization, The redundant data is deserialized and stored in the ExtensionData attribute, which resolves the problem of data loss.

We can test it, according to the sixth 1. The basic code of the data contract is tested.

Open the ListItem Data contract class under the ContentTypes project, commenting on the last property Url of the class

[8-01]

Then we re-build the whole project again, and then start Hosting, start Winfrom (don't update the service reference here first), enter the response string in the text box

Click the Save button, and then click the Get button.

This time you will find the string in the text box of the website you just entered disappears.

This is because the server is in response to the data contract you just changed, and the client is not updated with the service reference, it is used before the data contract to pass the data, the previous data contract has the URL attribute, but the server modified service contract There is no URL for this property.

That client I passed the value of the URL, why return to have no data?  Is your server version updated and I'm going to do the version update as well? In case a lot of other have downloaded your software, and because your server update, it will not be used? That's certainly not true.

In this case, the server version is updated, nothing more than the data contract has added attributes or deleted attributes. That my client is not updated, the version of the client is lower than the server version. The client passes a property that has been deleted by a server, you ignore it, but you must also return the value of the property that you ignored when the server returns.

Similarly, if the server added new properties, my client is the old version of the current data contract does not have the property, the client does not know that there is this property, do not pass, the service side of this property will be a null.

How to do it?

Let our data contract inherit the IExtensibleDataObject interface, and implement the interface

[8-02]

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Runtime.Serialization;6 7 namespaceContentTypes8 {9[DataContract (Namespace ="http://www.HulkXu.com/")]Ten     //[DataContract] One      Public classListitem:iextensibledataobject A     { -[DataMember (Name ="Id", isrequired =false, Order =0)] -         //[DataMember] the         Private Longm_id; -  -          Public LongId -         { +             Get{returnm_id;} -             Set{m_id =value;} +         } A         //[DataMember] at[DataMember (Name ="Title", isrequired =true, Order =1)] -         Private stringM_title; -  -          Public stringTitle -         { -             Get{returnM_title;} in             Set{M_title =value;} -         } to         //[DataMember] +[DataMember (Name ="Description", isrequired =true, Order =2)] -         Private stringm_description; the  *          Public stringDescription $         {Panax Notoginseng             Get{returnm_description;} -             Set{m_description =value;} the         } +         //[DataMember] A[DataMember (Name ="DateStart", isrequired =true, Order =3)] the         PrivateDateTime M_datestart; +  -          PublicDateTime DateStart $         { $             Get{returnM_datestart;} -             Set{M_datestart =value;} -         } the         //[DataMember] -[DataMember (Name ="DateEnd", isrequired =false, Order =4)]Wuyi         PrivateDateTime m_dateend; the  -          PublicDateTime dateend Wu         { -             Get{returnm_dateend;} About             Set{m_dateend =value;} $         } -  -  -         //Delete A         //[DataMember] +         //[DataMember (Name = "Url", IsRequired = False, Order = 5)] the         //private string M_url; -  $         //Public string Url the         //{ the         //get {return m_url;} the         //set {M_url = value;} the         //} -  in  the         //Add the         //[DataMember (isrequired=true)] About         //private string address; the         //Public string Address the         //{ the         //get {return address;} +         //set {address = value;} -         //} the         //Public Extensiondataobject ExtensionDataBayi         //{ the         //get; the         //set; -         //} -  the  the          PublicExtensiondataobject ExtensionData the         { the             Get; -             Set; the         } the  the 94     } the}

OK, this is all about data contract versioning, which is relatively straightforward.

Eighth Lecture: Data Contract version control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.