Based on Network Information and msdn, record how MTOM and WCF are applied.
1. Basic MTOM concepts
When it comes to MTOM message optimization transmission mechanism, the experimental result is that using MTOM to transmit data will increase the performance by about 33%. The MTOM standard allows you to externalize large data elements contained in a message and transmit them as binary data without any special encoding along with the message. MTOM messages are packaged into multi-part/related mime sequences and transmitted in a SOAP message.
We need to understand some related concepts. What is base64 encoding, MTOM message optimized transmission mechanism, and mime. These are necessary for us to understand the problem of MTOM message optimization transmission mechanism. The following is an introduction.
1.1 base64 encoding
The base64 encoding principle is very simple. The method is to take 6 bits for each input data stream (each bit represents 1 bit binary), and fill 0 with less than 6 bits, each 3 8-bit bytes is encoded into 4 6-bit bytes (3 × 8 → 4 × 6). If the number of 4 bytes is less than 4, it is filled with "=. In fact, these four six-bit bytes are still 8 bits, but the height of the two bits is set to 0. When a byte is valid for only 6 bits, its value space is 0 to the power of 6 of 2 minus 1, that is, 63, that is to say, the value space of each encoded base64 to be converted is (0 ~ 63 ). In this way, three 8-bit bytes can be converted to four bytes, and all the four converted bytes can be mapped to characters. That is, data can be replaced by character encoding. Because the converted string is 1/3 longer than the original one. Therefore, the encoded data length increases to 4/3 times. This is why soap messages are less efficient than MTOM messages. Because soap uses the XML language for message transmission, XML is a base64-Encoded Language.
1. 2 Mime
Mime indicates the multi-purpose Internet Mail Extension protocol. Mime allows a basic text-Oriented Internet mail system to include binary attachments in messages. Mime (Multipurpose Internet Mail Extentions), generally translated as "multi-purpose network mail extended protocol ". As the name suggests, it can transmit multimedia files. Mime (Multipurpose Internet Mail Extensions, multi-purpose Internet Mail Extension) is used to create e-mail exchanges, network documents, and other applications on the enterprise network and Internet.Program.
1.3 MTOM message optimized Transmission
MTOM stands for message transmission optimization mechanism. The model is applicable to the interaction of a large amount of data. Solution for the overhead caused by base64 encoding. When the data volume is small, soap still uses XML for message transmission.
However, in the case of a large amount of data, if the data is still base64 encoded, it will incur an additional 33% overhead. This situation is intolerable for a large amount of data exchanges. MTOM is an improvement method based on SOAP message transmission. For the transmission of a large amount of data, base64 encoding is not required. Instead, the Binary Raw Data of the attachment is directly encapsulated in the mime part of the SOAP message for transmission. A soap message references binary content by pointing to the mime part sent with it. In addition, it includes the basic XML data of soap, Which is base64-encoded. This model is basically the same as the SMTP model of the Simple Mail protocol.
MTOM improves data processing efficiency by simplifying the Encoding Process of a large amount of data. MTOM also has some necessary overhead because of necessary information such as soap messages. MTOM can only show its advantage when the size of binary data elements exceeds about 1 kb.
2. Example of MTOM encoding in WCF
This example demonstrates how to use message encoding with wshttpbinding in message transmission optimization (MTOM. MTOM is a mechanism used to transmit large binary attachments containing soap messages in the form of raw bytes, so that the transmitted messages are smaller.
By default, wshttpbinding sends and receives messages in normal text XML format. To allow sending and receiving MTOM messages, SetMessageencodingAttribute (example below)Code), Or useMessageencodingProperty is directly set in binding. The service or client can now send and receive MTOM messages.
< Wshttpbinding >
< Binding Name = "Wshttpbinding_iupload" Messageencoding = "MTOM" />
</ Wshttpbinding >
The MTOM encoder can optimize the array of bytes and streams. In the following exampleStreamParameters.
[Servicecontract (namespace = " Http://Microsoft.ServiceModel.Samples " )]
Public Interface Iupload
{
[Operationcontract]
Int Upload (stream data );
}
The Protocol selected for this example transmits binary data to the service and receives the number of uploaded bytes as the return value. When you run the client after installing the service, the service displays the number 1000, indicating that all 1000 bytes are received. The remaining output lists the optimized and unoptimized message sizes under various loads.
Output:
1000
Text Encoding with100Byte payload:433
MTOM encoding with100Byte payload:912
Text Encoding with1000Byte payload:1633
MTOM encoding with1000Byte payload:2080
Text Encoding with10000Byte payload:13633
MTOM encoding with10000Byte payload:11080
Text Encoding with100000Byte payload:133633
MTOM encoding with100000Byte payload:101080
Text Encoding with1000000Byte payload:1333633
MTOM encoding with1000000Byte payload:1001080
Press <enter>To terminate client.