WCF distributed development Step-by-step for Win (7): WCF Data Contract and serialization

Source: Internet
Author: User
Tags net serialization serialization

This section continues to learn WCF distributed development step-by-step for Win (7): WCF Data Contract and serialization. Data contract is an important concept in WCF application development, it is a very important step to realize the serialization of data contract between client and server. So what is serialization? Why is there a serialization mechanism? Or is it to solve any problem? What is the role? What is the difference between the existing. NET serialization mechanism and the WCF serialization mechanism? We will explain this in more detail in this section of the article. This section structure: "0" Data Contract "1" the basic concept of serialization "2". NET serialization mechanism "3" WCF serialization mechanism "4" Code Implementation and analysis "5" summary.

Here we formally enter the learning phase of today, first to introduce the concept of data contract:

"0" Data Contract (DataContract):

In WCF service programming we know that a service contract defines a remote access object and a service operation method that can be invoked, and a data contract that defines a custom data type to be transferred between the server side and the client. In a WCF project, declaring a type DataContract, the type can be serialized between the server and the client. A class can be transmitted only if it is declared as DataContract, and only the properties of the class are transferred, and the attribute is required to be datamember before the property life, so that the property can be serialized. The default Condition property is not transitive. The method of the class is not routed. WCF can have more detailed control over the type of data contract defined and can exclude a member property from the serialization scope, and the client program will not get any information about the excluded member properties, including definitions and data. The code is as follows:

[DataContract]//数据契约属性声明
     class MyDataContract
     {
         [DataMember(Name = "MyName")]//数据成员标记,支持别名定义
         public string Name
         {
             get;
             set;
         }
         [DataMember(Name = "MyEmail")]//数据成员标记,支持别名定义
         public string Email
         {
             get;
             set;
         }
         [DataMember]//数据成员标记
         public string Mobile
         {
             get;
             set;
         }
         //没有[DataMember]声明,不会被序列化
         public string Address
         {
             get;
             set;
         }
     }
}

The above class is declared as DataContract, and some attributes are declared as DataMember (data members). Can be serialized as a client delivery. The address member property is not declared as DataMember, so any information about the address is not transmitted when the data is exchanged. Members declared as DataMember can also customize client-visible aliases such as: [DataMember (Name = "myname")]//data member tags, which support alias definitions.

"1" Serialization of basic concepts:

Once you know some of the concepts and features of the data contract, here's how to do the serialization concept.

"1.1" Why serialization: Let's start by introducing why we need serialization. Of course this is not necessary. Only for specific development platform data or information types, when a system or platform needs to interact with other heterogeneous systems or platforms, the two systems need a specific publicly available industry standard to support the interaction of this data information. Now, the language vector that supports this data interchange is XML.

As a service-oriented programming framework, WCF is one of the goals or features of implementing services across languages, platforms, interacting with different services, without limiting the client's system or development language. To achieve this goal, WCF services first of all to face the issue of information transmission and sharing. We know that WCF services and clients can pass. NET data types such as int, string, and so on. However, it is a key issue how to implement user-defined cross service boundaries for custom complex types. Data contracts can be published as metadata for services, allowing clients to be translated into native language representations. The solution is to marshal (marshaling) and marshal the objects to other platforms. The process of passing client and server-side parameters based on WCF is shown below:

Master Step: The client serialization parameter is the XML information set--the delivery-> server-side deserialization to the local type--execution result-> serialization result to XML information set--pass-> Client order deserialization return information is local type.

In WCF Distributed Development Prerequisites (2): the. NET Remoting communication process is also described in the. NET Remoting section, both of which have a process and some similarities. The concept of object marshaling in fact. Net remoting early on, remote Objects (Remoteoject), that is, the objects we remotely want to access. First define a class, inherit MarshalByRefObject, can be used in the remoting application, Supports cross domain boundary access for objects. Read the. NET Remoting this article should also have a little impression, the difference is that WCF object marshaling is to cross the service boundary,. NET remoting is marshaled to cross cross-domain boundaries. Related concepts Check out WCF Distributed development Prerequisites (2):. Net Remoting or MSDN, you can find a detailed introduction, not detailed here.

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.