What is SOAP MTOM?

What is SOAP MTOM?
SOAP Message Transmission Optimization Mechanism
(MTOM) is the use of MIME to optimize the bitstream
transmission of SOAP messages that contain
significantly large base64Binary elements.
MTOM (Message Transmission Optimization Mechanism) is using for efficient
sending of binary codes to and from web services. The MTOM message format allows
bitstream compression of binary data. Data that would otherwise have to be encoded in
the SOAP message is instead transmitted as raw binary data in a separate MIME part.
A large chunk of binary data takes up less space than its encoded representation, so
MTOM can reduce transmission time, although it can increase processor usage.
Candidate elements to be transmitted in this way are defined as base64Binary in the
WSDL (XML Schema).
An MTOM message is identified by a Content-Type with a type of application/xop+xml.
The SOAP domain handles inbound MTOM messages automatically, and MTOM parts
are reincorporated automatically into the SOAP Body.
The use of outbound MTOM messages can be configured on the
SOAPReply , SOAPRequest , andSOAPAsyncRequest nodes.
How it works?
If you put Wireshark (or enabled System.Net Logging) on the non-MTOM enabled
service, you should see the SOAP requests with the binary data encoded as BASE64.
Sending it as BASE64 increases the size of the binary data but (I assume) makes it
more interoperable.
With MTOM, the SOAP messages are sent as MIME messages with the BASE64
encoding being replaced with a placeholder. The binary data is then placed between
delimiters (which happens for each piece of binary data), and then placed at the end of
the SOAP request. The binary data is then sent uuencoded. IIRC, MTOM also
determines whether sending it as a MIME message will increase the size of the SOAP
call and if doesn't provide a saving, it will send it as a normal SOAP message.
With web services-based SOA now being deployed across Global 2000
enterprises, transmitting attachments such as MRI Scans, X-Rays, Design Documents
and Business Contracts using SOAP messages has become a common practice.
SOAP Message Transmission Optimization Mechanism (MTOM), is a W3C
Recommendation designed for optimizing the electronic transmission of attachments.
Through electronic transmission of documents, corporations can realize significant cost
savings and better service levels by eliminating the use of postal mail. Paper-based
manual tasks can be replaced with simple and efficient electronic processes where
binary data can be transmitted between organizations through standards such as
MTOM.
MTOM provides an elegant mechanism of efficiently transmitting binary data, such
as Images, PDF files, MS Word documents, between systems. The Figure below shows
the steps involved in transmitting data between a Consumer and Producer using
MTOM.
The Consumer application begins by sending a SOAP Message that contains complex
data in Base64Binary encoded format. Base64Binary data type represents arbitrary
data (e.g., Images, PDF files, Word Docs) in 65 textual characters that can be displayed
as part of a SOAP Message element. For the Send SOAP Message Step 1 in the Figure
above, a sample SOAP Body with Base64Binaryencoded element <tns:data> is as
follows:
<soap:Body>
<tns:ByteEcho>
<tns:data>JVBERi0xLjYNJeLjz9MNCjE+DQpzdGFyNCjEx0YNCg==</tns:data>
</tns:ByteEcho>
</soap:Body>
An MTOM-aware web services engine detects the presence of Base64Binary encoded
data types, <tns:data> in this example, and makes a decision – typically based on data
size – to convert theBase64Binary data to MIME data with an XML-binary optimization
Package (xop) content type. The data conversion, shown in Step 2 of the Figure above,
results in replacing the Base64Binary data with an <xop:Include> element that
references the original raw bytes of the document being transmitted. The raw bytes are
appended to the SOAP Message and are separated by a MIME boundary as shown
below:
<soap:Envelope>
<soap:Body>
<tns:ByteEcho>
<tns:data><xop:Include
href="cid:[email protected]"/></tns:data>
</tns:ByteEcho>
</soap:Body>
</soap:Envelope>
--MIMEBoundary000000
content-id: <[email protected]>
content-type: application/octet-stream
content-transfer-encoding: binary
The raw binary data along with the SOAP Message and the MIME Boundary is
transmitted over the wire to the Producer. The Producer then changes the raw binary
data back to Base64Binary encoding for further processing. With this conversion
between Base64Binary and raw binary MIME types, MTOM provides two significant
advantages:
1. Efficient Transmission: Base64Binary encoded data is ~33% larger than raw byte
transmission using MIME. MTOM therefore reduces data bloat by converting
Base64Binary encoding to raw bytes for transmission.
2. Processing Simplicity: Base64Binary encoded data is composed of 65 textual
characters. The data is represented within an element of a SOAP message.
Security standards such as WS-Signatures and WS-Encryption can directly be
applied to the SOAP Message. Once such operations are performed,
theBase64Binary data can be converted to raw bytes for efficient transmission.
Securing document transmission via SOAP, therefore, does not require
additional standards for securing MIME-based attachments.
Testing service without MTOM and with MTOM
For a hands-on understanding of MTOM, we will build web services with simple
operation that will echo a byte stream:
To build a simple web service that illustrates handling binary data, can install Microsoft
WebMatrix and create a simple web service:
Note: The OK button will be grayed until all the information on this panel is filled
out.
This will auto-generate a web service for you with an Add(int a, int b) operation
as shown below. We will keep this auto-generated operation and include a new
byte processing operation in the next step.
1. Cut and paste the following code in the Web Matrix IDE right under the Add(int a,
int b) operation:
[WebMethod]
public byte [] ByteEcho(byte[] data) {
return data;
}
2. The IDE will look as follows:
3. Hit the play button in the IDE and it will prompt you for the start the web
application on port 8080. Your local firewall may prevent you from starting a
listener on port 8080. Add the port to your firewall's list of allowable ports.
4. A web browser with operation Add and ByteEcho will appear. You can click this
and start playing with the Add operation. The ByteEcho operation does not
accept input from the browser.
SOAPSonar SETUP
To setup the SOAPSonar test client, perform the following steps:
1. Goto: Start > All Programs > Crosscheck Networks > SOAPSonar Enterprise
Edition 3 > SOAPSonar Enterprise to launch SOAPSonar.
2. Load the WSDL published at the .NET WebMatrix
Endpoint http://localhost:8080/BinaryProcess.asmx?WSDL into SOAPSonar as
shown in the figure below. Select the ByteEcho_1 test case in the left Project
Tree Panel. Right click on the data field and select Content Function >
Base64Encode. File Dialog will pop up to enable you to select a file from your
system such as a PDF or JPEG file. The filename will be embedded in the data
field with a$b64:file prefix. Click
to commit changes and
to execute the
test. You will see the SOAP response in the Response Panel.
3. Save the project by going to File > Save Project As.
4. Click
on in the Request Panel to review the Header information for the
request. Select the (Sent Request) Tab as show in the Figure below. The SOAP
request and the Header information are also shown below. Make a note of the
following information:
a. Header Content-Length is 324771 bytes. This will vary based on the file
you select.
b. Content-type is text/xml. This will change when MTOM is enabled.
c. The data field contains base64Encoded value of the selected binary file.
So far, you have successfully loaded a WSDL into the test client, SOAPSonar and setup
a simple consumer (SOAPSonar) to producer (webMatrix) Framework to send
base64Encoded bytes to the webMatrix server that reflects the bytes to SOAPSonar in
the SOAP Response. Next, we will enable MTOM and review its impact on the SOAP
Request and Response.
ENABLING MTOM
In the section, we will enable MTOM for WebMatrix and the SOAPSonar test client. To
enable MTOM for webMatrix as follows:
1. Goto C:\Program Files\Microsoft WSE\v3.0\Tools and launch
WseConfigEditor3.exe.
2. With the WSE 3.0 Configuration tool, select the File Menu to open
the web.config file located
inC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG.
3. Under the General Tab, check both boxes to enable WSE 3.0 for the WebMatrix
sever as shown in the Figure below:
4. Next, select the Messaging Tab and select “always” for the Server Mode MTOM
Settings.
5. Goto File > Save to the new configuration and Exit from the WSE 3.0
configuration tool.
6. Review the web.config file using a text editor and ensure that the following
elements appear in the web.config file. As expected, the serverMode value for
MTOM is set to “always.” The server will now only accept MTOM encoded
messages. If a SOAP request is received by WebMatrix that is not MTOM, an
HTTP error 415: "Media unsupported" is returned to the sender.
<messaging>
<mtom clientMode="Off" serverMode="always"/>
</messaging>
7. Re-send the base64Encoded SOAP request. Since WebMatrix receives a
message that is not MTOM, an HTTP error 415: "Media unsupported" is returned
to the SOAPSonar as displayed in the Response Panel.
8. In the Project Tree, click on the Policy Settings node. Change the MTOM Setting
– Client Mode to On as shown in the Figure below. This enables SOAPSonar to
send MTOM encoded messages. Click to commit changes.
9. Goto the ByteEcho_1 test case and click
to execute the test. Review the
request sent to the server by clicking on the (Send Request) Tab in the Request
Panel. The SOAP request and the Header information are also shown below.
Make a note of the following information:
a. Header Content-Length is 244093 bytes. This will vary based on the file
you select.
b. Content-type is application/xop+xml. This indicates that an MTOM
message is being generated by SOAPSonar.
c. The data field contains MIMEBoundary content-transfer-encoding.
Note: The Header Content Length for the Message Request with MTOM turnedon is 244K compared to 325K with the Message Request without MTOM. This
corresponds to a ~25% reduction in message size even for a moderately sized
message.
10. Goto the panel shown in Step 8 above and check Show Raw Response for the
MTOM settings. This turns-off the binary to text-encoding and enables you to
view the raw binary content in the response panel.
You should be now be comfortable with sending base64Binary encoded and MTOM
encoded messages to a test server and viewing the responses in wire format and
base64Binary encoded formats.
Conclusion
MTOM provides an efficient mechanism for transmitting binary data. MTOM’s approach
of reducing the number of standards required for transmission while reducing the data
bloat caused byBase64Binary encoding the entire attachment makes it an ideal
standard for transmission of large binary content. Obviously, nothing comes for free.
The gain in network transmission efficiency by reducing “wire footprint” is at the
expense of CPU resources. The client has to expend processing resources to
convert Base64Binary encoded type to MIME type before transmitting it over the wire.
The receiver then performs another data type transformation from MIME
to Base64Binary data. MTOM is ideal for use cases where a large number of external
organizations want to transmit sizeable documents to an enterprise over the internet
with low bandwidth availability.
References
1. Faster Data Transport Means Faster Web Services with MTOM/XOP.
2. XML, SOAP and Binary Data.
3. Getting Started: Creating a WSE 3.0 Enabled Web Service to Transfer Large
Amount of Data using WSE 3.0 MTOM.
4. www.crosschecknet.com
5. http://publib.boulder.ibm.com
6. http://en.wikipedia.org/wiki/Message_Transmission_Optimization_Mechanism
7. http://www.crosschecknet.com/intro_to_mtom.php
8. http://stackoverflow.com/questions/215741/how-does-mtom-work
9. http://www.w3.org/TR/soap12-mtom/