This is a showcase review for our sponsors at The Code Project. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.
Learn how to use MTOM by building a simple prototype.
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 and Microsoft 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. The 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 Base64Binary encoded element
<tns:data> is as follows:
An MTOM-aware web services engine detects the presence of Base64Binary encoded data types,
<tns:data> in our example, and makes a decision that is typically based on data size to convert the Base64Binary 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:
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:
- 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.
- 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, the Base64Binary 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.
In this paper, we will deploy a web service for processing binary data
ByteEcho(byte data) and examine using this service without MTOM and with MTOM enabled.
Setup and Installation Overview
For a hands-on understanding of MTOM, we will build web services with a simple operation that will echo a byte stream. In this section, we will focus on setting up the SOA Testing framework for MTOM by installing the following components. Install the three products below in the listed sequence:
- Crosscheck Networks SOAPSonar Enterprise Edition: A .NET-based SOAP client used for comprehensive web services testing. Install prerequisites that the SOAPSonar installer will ask you for, such as .NET Framework 2.0 if it is not already installed on your machine.
- Web Service Enhancements (WSE) 3.0 for Microsoft .NET Framework: This is an add-on to Microsoft .NET Framework 2.0 that enables developers to build secure Web services based on the latest Web services protocol specifications such as MTOM. Note: During installation, please select the Developer Version option.
- Microsoft .NET WebMatrix: This installer includes an IDE for building web services and a lightweight web server.
All three components can be installed on a Windows 2000/XP/2003/Vista machine with moderate resources. The web services Producer is the .NET WebMatrix server that supplies a web service with the
ByteEcho(byte data) operation that applications can invoke over HTTP. In addition, it produces the WSDL file that defines the web service interface. This file provides all the necessary information for the consumer, SOAPSonar, to send SOAP requests to the target web service. SOAPSonar consumes and interprets the WSDL-based API published by the producer and invokes the web service.
Building a Web Service for Binary Data
To build a simple web service that illustrates handling binary data, follow these steps:
Goto: Start > All Programs > Microsoft ASP .NET Web Matrix > ASP .NET Web Matrix.
You will be prompted with the screen shown below. Fill in the information as shown. Select "Web Services" in the left panel and the "Simple" template in the right panel. Create a C:\WebServices folder to store the *.asmx file and picked C#.
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.
Cut and paste the following code in the Web Matrix IDE right under the
Add(int a, int b) operation:
public byte  ByteEcho(byte data)
The IDE will look as follows:
Hit the Play button in the IDE and it will prompt you to 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.
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.
To setup the SOAPSonar test client, perform the following steps:
Goto: Start > All Programs > Crosscheck Networks > SOAPSonar Enterprise Edition 3 > SOAPSonar Enterprise to launch SOAPSonar.
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
Click to commit changes and to execute the test. You will see the SOAP response in the Response Panel.
Save the project by going to File > Save Project As.
Click in the Request Panel to review the Header information for the request. Select the (Sent Request) Tab as shown in the Figure below. The SOAP request and the Header information are also shown below. Make a note of the following information:
- Header Content-Length is 324771 bytes. This will vary based on the file you select.
- Content type is text/XML. This will change when MTOM is enabled.
- The data field contains the base64Encoded value of the selected binary file.
So far, you have successfully loaded a WSDL into the test client, SOAPSonar and set up a simple consumer (SOAPSonar) to producer (webMatrix) Framework which sends 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.
In the section, we will enable MTOM for WebMatrix and the SOAPSonar test client. Enable MTOM for webMatrix as follows:
Goto: C:\Program Files\Microsoft WSE\v3.0\Tools and launch WseConfigEditor3.exe.
With the WSE 3.0 Configuration tool, select the File Menu to open the web.config file located in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG.
Under the General Tab, check both boxes to enable WSE 3.0 for the WebMatrix sever as shown in the Figure below:
Next, select the Messaging Tab and select “always” for the Server Mode MTOM Settings.
Goto: File > Save to the new configuration and Exit from the WSE 3.0 configuration tool.
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.
<mtom clientMode="Off" serverMode="always"/>
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.
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.
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:
- Header Content-Length is 244093 bytes. This will vary based on the file you select.
- Content-type is application/xop+xml. This indicates that an MTOM message is being generated by SOAPSonar.
- The data field contains MIMEBoundary content-transfer-encoding.
Note: The Header Content Length for the Message Request with MTOM turned-on 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.
Go to 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.
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 by Base64Binary 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 the Base64Binary encoded type to the 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 in cases where a large number of external organizations want to transmit sizeable documents to an enterprise over the internet with low bandwidth availability.
- Faster Data Transport Means Faster Web Services with MTOM/XOP
- XML, SOAP and Binary Data
- Getting Started: Creating a WSE 3.0 Enabled Web Service to Transfer Large Amount of Data using WSE 3.0 MTOM