WCF Technical Analysis of 19: Depth analysis of message encoding (Encoding) implementation (last article)

Source: Internet
Author: User
Tags bool

Messages are the only medium for WCF to communicate, and ultimately need to be passed through the write transport layer. A prerequisite for the transmission of a message, or an essential task, is to encode the message accordingly. WCF provides a range of alternative coding options, each of which has an advantage in interoperability and performance. In this article we will discuss the various coding methods for the message.

From an interoperability perspective, the coding approach largely determines the ability to support across platforms. Some of the coding methods are platform-independent, others are limited to a particular platform. WCF provides 3 typical encoding methods: Binary, Text, and Mtom. Binrary the encoding of messages in binary mode, but is limited to. NET platform, text provides a platform-independent text-based encoding method. MTOM coding based on Ws-mtom specification is of great significance for improving the transmission performance of large scale binary data in the SOAP message, since the coding method follows the corresponding specification, which is a cross-platform coding method undoubtedly.

Before we formally introduce WCF message encoding, it is important to understand the following core objects of implementation coding: Xmldictionary, Xmldictionary, and XmlDictionaryWriter.

First, Xmldictionary

Xmldictionary, as the name suggests, is a dictionary that is engaged in encoding and decoding both sides shared a "glossary". This may be a bit abstract, so we might as well make an analogy. For example, I said, "WCF is a SOA-based messaging framework under the. NET Platform", which is well understood by readers. If I say this to another person who doesn't know anything about computers, there is no doubt that the other side is somehow incomprehensible. The reason why readers and I can communicate through this language is because we have similar knowledge backgrounds, share the same glossary between us, and have a consistent understanding of the meaning of each word. and others can't understand, it is the asymmetric information between me and him, and if it is to be understood, I must communicate in a way that he can understand, in which case I may have to spend a lot of words explaining the terms of the sentence in detail, such as what is. NET platform, what is SOA, and what is the communication framework. So, the premise of communication is that both sides have the same "glossary", the more the two sides on a topic to share the more "vocabulary", the easier communication, the more concise you say.

The code of the data is also like our daily communication and communication, the coding side is the "say" side, the decoding party is the "listening" side. The said party encodes the information according to the "Glossary" it holds, and the other side only has the same "glossary" to decode it properly. The more detailed the glossary, the smaller the amount of content that is encoded. What does the concentration of content mean? means that the reduction in network traffic means that you are saving more bandwidth. And xmldictionary is such a glossary.

Xmldictionary is defined under the System.Xml namespace, which is a collection of System.Xml.XmlDictionaryString. The xmldictionarystring corresponds to a Keyvaluepair<int,string> object, is a key-value pair, and the keys and values are of type int and string. The following is the definition of xmldictionarystring and xmldictionary.

 1:public class XmlDictionaryString 
2: {
3://other member
4:public xmldictionarystring (ixmldi Ctionary dictionary, string value, int key);
5:public ixmldictionary Dictionary {get;}
6:public static xmldictionarystring Empty {get;}
7:public int Key {get;}
8:public string Value {get;}
9:
:
1:public class Xmldictionary:ixmldictionary
2: {
3:
4://Other member
5: public Xmldictionary ();
6:public xmldictionary (int capacity);
7:public Virtual xmldictionarystring Add (string value);
8:
9:public virtual bool Trylookup (int key, out xmldictionarystring result);
10:public virtual bool Trylookup (string value, out xmldictionarystring result);
11:public virtual bool Trylookup (xmldictionarystring value, out xmldictionarystring result);
A:}

A Xmldictionary object was created with the following code, and 3 xmldictionarystring were added through the Add method. Strictly speaking, Xmldictionary is not a collection object because it does not implement the IEnumerable interface. By using the Add method you can only specify that the value of the XmlDictionaryString Value,key is automatically assigned in the form of growth. So the key for the customer, Name, and company 3 elements is 0,1,2, which can be seen from the final output.

   1:ilist<xmldictionarystring> dictionarystringlist = new list<xmldictionarystring> ();
2:xmldictionary dictionary = new Xmldictionary ();
3:dictionarystringlist.add (dictionary. ADD ("Customer"));
4:dictionarystringlist.add (dictionary. ADD ("Name"));
5:dictionarystringlist.add (dictionary. ADD ("Company"));
6:foreach (xmldictionarystring dictionarystring in dictionarystringlist)
7: {
8: Console.WriteLine ("Key:{0}\tvalue:{1}", Dictionarystring.key, Dictionarystring.value);
9:}

Output results:

   1:key:0    Value:customer
2:key:1 Value:name
3:key:2 Value:company

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.