"WCF Technology Insider" 21:2nd Part _ 5th Chapter _ Message: WCF XML Stack and Xmldictionary

Source: Internet
Author: User
Tags constructor soap requires

WCF XML Stack

The Microsoft. NET Framework defines a rich collection of types for various purposes of XML processing. As a messaging platform, WCF requires more normal functionality than other. NET applications. For example, as you can see in Chapter 2nd: "Service oriented," WCF is able to generate, send, accept, process binary or MTOM encoded XML messages. Because the. NET framework does not provide these capabilities, the WCF API itself defines types to implement these functions, and we can use these types to interact directly with the message type. In other words, the type defined by the WCF API can convert the message type to a specific encoding format. Under the WCF System.Xml namespace for the System.Runtime.Serialization.dll assembly, there are three key type definitions that are responsible for serializing and encoding the message type: Xmldictionary, XmlDictionaryWriter and XmlDictionaryReader. For the sake of discussion as simple as possible, I use XML message fragments to illustrate these types, rather than using fully formatted SOAP messages. In the later part of this chapter, you will see how these types are serialized and encoded by the message type.

Xmldictionary type "Old Xu Notes 1"

As the meaning of its name, a Xmldictionary object is a map of many pairs of key-value. Much like a dictionary or glossary, xmldictionary can be used to replace simple expressions without losing any meaning. We often use this mechanism in our daily life, but we are not aware of its existence. Consider the following I said to my friend Rusty: "I saw a movie about submarines last night." "Rusty will hear the news and explain," I saw a movie last night about a container that could sail on water and underwater. "The first sentence is much simpler than the second sentence, and it requires less time to express," she said. This compression and correlation processing time can be saved because I share a glossary with Rusty. As long as rusty and I understand the same words, the two of us can communicate effectively. If I say to Rusty, "this chapter is finally done," elucubration, "and he doesn't know what I'm talking about." In this example, I used a rusty word to save time. Therefore, a dictionary (or a glossary) that all participants know will improve efficiency.

Risking the death of this metaphor, there is one point that it can prove. When I said to Rusty, "I saw a submarine movie last night," the whole sentence itself can be expressed in several different ways and languages. If you know the meaning of a movie (movie) and a submarine (submarine), you might think of a scene like this, a dark theater (which can smell 5 dollars of popcorn) and a submarine on the screen. In other words, these words in the sentence stirred up some pictures in the real world. On XML infoset and coding, you can map XML infoset to "things" in the real world, and you can also convert these words into specific encodings.

In messaging applications, xmldictionary may be used to compress serialized and encoded message sizes, thereby reducing the bandwidth required for messages to be sent. Just as people must have a common vocabulary before efficient communication, both the sender and the recipient must use a compatible Xmldictionary object when exchanging messages. Internally, Xmldictionary defines a private list of key-value pairs that can represent the names, attributes, and XML namespace declarations of the elements in a SOAP message.

Before using xmldictionary directly, it is necessary to study the data stored in the Xmldictionary instance in detail. The key-value pairs of xmldictionary internal storage are xmldictionarystring types. A xmldictionarystring type defines a Int32 key property and a string type value. Although XmlDictionaryString defines a common constructor. XmlDictionaryString is not created directly by user code, but by adding xmldictionarystring objects to xmldictionary instances. (We'll see examples of xmldictionarystring in the later part of this section.) )

Xmldictionary defines an parameterless constructor and a rarely used constructor that can accept Int32 numbers representing the maximum number of xmldictionarystring elements. After the construction instance is finished, xmldictionarystring can add to the xmldictionary by invoking the instance's Add method. The Add method takes a string type and returns a XmlDictionaryString instance, as follows:

XmlDictionary dictionary = new XmlDictionary();
List<XmlDictionaryString> stringList = new  List<XmlDictionaryString>();
// add element names to the dictionary and store in  stringList
stringList.Add(dictionary.Add("ReleaseDate"));
stringList.Add(dictionary.Add("GoodSongs"));
stringList.Add(dictionary.Add("Studio"));

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.