We all know that WCF is composed of a channel stack, before we transmit the parameters to the transmission channel layer, we need to go through the serialization process, that is, the parameter is serialized into a message, this article
let's just say Here's the serialization, pretty interesting, probably beginners also understand that in WCF The default serialization in the DataContractSerializer is , that's true, but WCF in the channel
actually not only support DataContractSerializer , it also supports holding its serialization of his type, such as XmlSerializer , NetDataContractSerializer as well Datacontractjson
Serializer , below we together to witness the next.
1. XmlSerializer
To understand XmlSerializer, let's start with a simple look at NetDataContractSerializer, in the previous article, I also said DataContract is to be our Model serialized to
XSD , the 2nd is is to use DataContract the principle is you must be in Model on Add DataContract , and in the field you want to serialize, add DataMember . so that the correct sequence can be
for the sake of first, let's look at the default serialization Model What would it be like?
1 [DataContract]2 Public classStudent3 {4 [DataMember]5 Public intID {Get;Set; }6 7 [DataMember]8 Public stringName {Get;Set; }9 Ten [DataMember] One Public stringSNS {Get;Set; } A}
But in some cases, you may not be suitable for DataContract, such as model is provided by a third party, so this time your model There may be no datacontract mark, and that 's
words WCF cannot be serialized, if I have to make sure WCF If you can run normally, is there any other good way??? Of course, there must be a way, it is like falling in love, always can not
Hanging in a tree to die, no one who can not leave who will die, the end of the earth where there is no grass, man who has no wife, right. WCF as well, since DataContract with no , since
Then there will be someone to replace it, and that person is XmlSerializer . , it is also very simple to use, that is, in the contract method above add XmlSerializerFormat, and then we put the Model 's
DataCo ntract remove All.
is not very simple, we need to verify the following, see if this format into the operation behavior ,
As you can see from the above diagram, XmlSerializerFormat has been injected into the Behavior and is handled by the class Xmlserializeroperationbehavior on its behalf.
Next, we monitor with fiddler to see if the Body of the Message really follows XmlSerializer Serialized.
Have not seen, this time message body and the article at the beginning of the Message is not the same.
2. Netdatacontract
This thing also has nothing to say, the light on the surface, it and DataContract The only different place is more than one Net, so you can probably guess, this function probably and Datacont
ract like, just more than DataContract More an assembly save, then what does this sentence mean??? Is that netdatacontract will save the assembly's namespace and class name to the XSD ,
The same assembly must be used to solve the deserialization process, in fact, whether we are doing SOA or object-oriented programming is about interface coding process, while netdatacontract give you the impression that is face
programming to objects, of course, is also beneficial, for example, if you take the assembly in like a secret key, you must have it to unlock it. Yes, that's why. WCF The project team does not netdatacontract Colds
, so it is not recommended to use it in practical applications.
3. DataContractJsonSerializer
See the above with the word Json , I think we all know what this thing is doing??? Yes, he just serialized our Model into Json, which in the rest code of WCF the use of a very wide,
If you are interested, I will describe in detail in the next article, here we take a brief look at first.
OK, this one says this, wash and sleep ...
15 days Proficient wcf--12th Day talk about the types of serialization in WCF