15 days proficient in WCF-12th days talk about the serialization of wcf, and the serialization of wcf

Source: Internet
Author: User

15 days proficient in WCF-12th days talk about the serialization of wcf, and the serialization of wcf

  

We all know that wcf is composed of channel stacks. Before we transmit parameters to the transport channel layer, We need to serialize the parameters to message.

The serialization here is very interesting. It may be clear to beginners that the default serialization in wcf is DataContractSerializer. This is true, but in the Channel

In fact, it not only supports DataContractSerializer, but also supports other types of serialization, such as XmlSerializer, NetDataContractSerializer, and DataContractJson

Serializer. Let's witness it together.

 

1. XmlSerializer

To learn about XmlSerializer, let's take a brief look at NetDataContractSerializer. in the previous article, I also said that DataContract serializes our model

XSD. The second point is to use DataContract. The principle is that you must add DataContract to the Model and add DataMember to the field to be serialized. In this way, the correct sequence can be obtained.

For demonstration, Let's first look at what the default serialization Model will look like?

 1     [DataContract] 2     public class Student 3     { 4         [DataMember] 5         public int ID { get; set; } 6  7         [DataMember] 8         public string Name { get; set; } 9 10         [DataMember]11         public string SNS { get; set; }12     }

However, in some cases, you may not be suitable for using DataContract. For example, if the Model is provided by a third party, your Model may not have the DataContract mark at this time.

In this case, wcf cannot be serialized. If I want to ensure that wcf can run properly, is there any other good way ??? Of course, there must be a way. This is like falling in love.

Hanging on a tree, no one can do without anyone, no one will leave, no one will die, no grass in the end of the world, how the man suffers from no wife, right. The same is true in Wcf. Since DataContract cannot be used

However, there will be someone who will replace it. The person will be XmlSerializer and it will be very easy to use, that is, add XmlSerializerFormat to the contract method, and then we will put the Model

Remove all DataContract.

 

Isn't it easy? Let's verify it and see if the Format has entered the Operation Behavior,

 

As shown in the preceding figure, the XmlSerializerFormat has been injected into the Behavior and is processed by the XmlSerializerOperationBehavior class.

 

Next, we will use fiddler to monitor whether the Body in the Message is actually serialized according to XmlSerializer.

Have you seen that the Body of this Message is different from the Message at the beginning of the article.

 

2. NetDataContract

There is nothing to say about this. On the surface, the only difference between it and DataContract is the addition of a Net, so you can probably guess that this function is probably different from DataCont.

Just like ract, there is only one more assembly to save than DataContract. What does this mean ??? That is, NetDataContract saves the namespace and Class Name of the assembly to XSD,

In the deserialization process, the same assembly must be used to solve the problem. In fact, no matter whether we are doing SOA or object-oriented programming, we pay attention to interface programming. NetDataContract gives you the impression that

Programming with objects, of course, is also advantageous. For example, if an assembly is brought in like a key, it must be included before it can be unlocked, right? As a result, the wcf project team does not catch a cold with NetDataContract.

Therefore, it is not recommended for practical applications.

 

3. DataContractJsonSerializer

I think everyone knows what the above Json text is ??? That's right. He just serializes our Model into Json, which is widely used in the rest coding of wcf,

If you are interested, I will describe it in detail in the next article. Here we will take a simple look.

 

Okay, that's all about it. You have to wash your bed...

 

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.