My journey to WCF (4): serialization in WCF [Part 1]

Source: Internet
Author: User

SOA and Message

Windows Communication Foundation (WCF) is an ideal Distributed Technology based on Service Orientation Architecture-SOA ), we believe that there will be much to do in the future in building SOA-based enterprise-level solutions and system integration. A SOA-based interconnected System is usually composed of several Sub-systems, which may be an independent Application, it may also be a small system where several applications integrate with each other to complete a group of related tasks. These subsystems are combined and integrated in an effective way as a comprehensive solution.

In an SOA-based distributed system, subsystems are independent and correlated. They are independent because each of them is an Autonomous System that can implement their own version policies and deployment policies, changes in the deployment of such versions should not cause changes in other parts of the system. They are related to each other because the functions required by a sub-system are often implemented by another sub-system. We call the feature Provider as the Service Provider ), call one party using the Service as a Client ). The Client depends on Service calls without losing its dependence on Service implementation. As long as the call method of the Service is not changed, Service implementation changes are completely transparent to Service users. In WCF, we extract Service call-related information as Contract, which we often call, service providers and clients share Service Contract, rather than Type in the traditional OO concept. Loosely Couple has long been applied to the distribution of relatively stable Service Contract and constantly changing Service Implementation ).

We have briefly introduced the basic features of the SOA system-Loosely Couple among subsystems, Autonomous of each subsystem, and shared Contract. In addition, SOA has other features. The most important feature is a Message-Based system. The interaction between subsystems is implemented by Message. The Client sends a Soap Message to the Service provider to access the Service required by the Client. The Service provider listens to requests from the Client, creates the corresponding Service object, and performs related operations, return the Result to the corresponding Client in the form of Message. Therefore, we can say that the interaction between subsystems is essentially a Message Exchange process ). Different interaction Methods correspond to different Message Exchange Pattern -- MEP.

After understanding the basic principles of SO, let's take a look at WCF and analyze it from the full name of WCF-Windows Communication Foundation, as its name implies, in other words, it actually provides an Infrastructure (Infrastructure) to implement Application Communication. As we mentioned earlier, subsystems interact with each other through XML Message, so we can regard WCF as a framework for completely processing XML Message, all functions of WCF are centered around Message-how to call a Service or call it a Message Exchange (Service Contract); how to implement general. NET object and XML Infoset (Serialization and Deserialization) that can be contained in XML Message; how to implement XML Infoset that carries data and Byte Stream that can be used for network transmission (Byte Stream) mutual conversion between (Encoding and Deconding); how to ensure data consistency in the Message and prevent malicious user theft, and verify the validity of calling the Service and passing the Service (Security: Confidentiality, Integrity, A Uthentication-CIA); how to ensure that the Message is reliably delivered to the desired location (Reliable Messaging ); and how to include several Service calls-essentially several Message exchanges into a separate Conversation (Session Support and Transaction Support ......

In a distributed system, data must be carried when an Application interacts with another Application. As we mentioned above, system interaction is performed in the form of Message, and the Message is XML. Of course, the data placed in the Message should also be XML (XML Infoset ). To process these interactive data, we may first think of directly processing XML. At the XML level, we can use the XML technology-XSD, XPath, and XSLT to operate the data. However, to make the XML after processing completely consistent with the requirements, such a job is boring and time-consuming and laborious. What we are best at is using. NET objects to encapsulate our data. How to effectively convert the created object into a structured XML Infoset is what we will talk about today-Serialization.

Serialization V. S. Encoding

Serialization can be seen as converting data containing the same content from one structure (. NET Object) to another structure (XML ). To realize the conversion between two different structures, there must be a Mapping between these two structures. Serialization is implemented by the Serializer. Serializer uses a certain algorithm (Arithmetic) to provide such Mapping. We know the structure information of a Managed Type, such as the list of all its members, the Type and access restriction of each member, and the attributes on each member, as the original data is stored in the original data table of the Assembly, the original data can be obtained through the reflection mechanism. XML structures are generally defined using XSD. Therefore, Serialization in WCF can be considered as a Serializer that analyzes the original data of the Type corresponding to the object through the reflection mechanism, thus providing an algorithm for converting the XSD of the Managed Type.

Many people who are new to WCF often cannot distinguish Serialization from Encoding. Our. NET Object is converted to XML Infoset through Serialization. However, to make our data pass over the network protocol, we must convert the generated XML Infoset into Byte Stream ). Therefore, Encoding focuses on the conversion process from XML Infoset to Byte Stream. In WCF, there are 3 different methods available: Binary; Text and MTOM (Message Transmit Optimized mechanic ). Binary has the best Performance, Text has the best interoperability, and MTOM is conducive to the transmission of a large amount of data.

We can understand Serialization and Encoding in this way. Sterilization is based on Service Contract. In fact, it is also defined in Service Contract and placed in our Code. However, Encoding is generally provided by Binding, it has nothing to do with the Service. We generally choose the appropriate Encoding in the Configuration according to our actual needs. It is advantageous for WCF to separate Serialization and Encoding from each other. The Serialization mobile deployment environment has a relatively low impact and is relatively universal, while Encoding is related to Service access performance and interoperability, it is closely related to the deployment environment. For example, a system used within an Intranet is often in consideration of improving Performance. We generally use TCP Transport in combination with Binary, which may result in potential calls from the Internet one day, we have to use Http as Transport and Text Encoding. Since Encoding is configurable, in this case, we only need to change the Configuration file.

DataContractSerializer

Serialization is completed through Serializer. In WCF, we have three different Serializer -- DataContractSerializer (defined in System. runTime. serializtion namespace), XMLSerializer (defined in System. XML. serialization namespace) and NetDataContractSerializer (defined in System. XML. serialization namespace ). They implement the Serialization of. NET Object in different ways. Since DataContractSerializer and NetDataContractSerializer are basically not much different, we will only discuss DataContractSerializer and XMLSerializer. DataContractSerializer is the default Serializer of WCF. If another Serializer is not explicitly used, WCF will create a DataContractSerializer to serialize the NET Object. First, we will discuss a Mapping method used by DataContractSerializer to convert. NET Object into XML. We use experiments as an example to illustrate the problem.

We create two classes of DataContractProduct and DataContractOrder to indicate the two entities of the product and the order. The reader can know the content of the description completely, which is not particularly described here.

 

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. Serialization;

Namespace Artech. WCFSerialization
{
[DataContract]
Public class DataContractProduct
{
Private Fields # region Private Fields
Private Guid _ productID;
Private string _ productName;
Private string _ producingArea;
Private double _ unitPrice;
# Endregion

Constructors # region Constructors
Public DataContractProduct ()
{
Console. WriteLine ("The constructor of DataContractProduct has been invocated! ");
}

Public DataContractProduct (Guid id, string name, string producingArea, double price)
{
This. _ productID = id;
This. _ productName = name;
This. _ producingArea = producingArea;
&

Related Article

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.