Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Introduction to MTOM: A Hands-on Approach

3 Jan 2008 1  
Learn how to use MTOM by building a simple prototype.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

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.

Introduction to MTOM: A Hands-on Approach

Learn how to use MTOM by building a simple prototype.

MTOM Overview

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.

MTOM Process

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:

<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 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:

<soap:Envelope>
    <soap:Body>
        <tns:ByteEcho>
        <tns:data><xop:Include href="cid:1.633335845875937500@example.org"/></tns:data>
        </tns:ByteEcho>
    </soap:Body>
</soap:Envelope>

--MIMEBoundary000000
content-id: <1.633335845875937500@example.org>
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, 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:

  1. 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.
  2. 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.
  3. 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:

  1. Goto: Start > All Programs > Microsoft ASP .NET Web Matrix > ASP .NET Web Matrix.

  2. 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#.

    Create a New File in WebMatrix

    Note: The OK button will be grayed until all the information on this panel is filled out.

  3. 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.

    WebMatrix C# Code
  4. 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;
    }
  5. The IDE will look as follows:

    WebMatrix Code for Processing Binary Data
  6. 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.

    Screen Shot to show WebMatrix Server Starup on Application Port
  7. 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 SOAPSonar Save to commit changes and SOAPSonar Run to execute the test. You will see the SOAP response in the Response Panel.

    SOAPSonar Load WSDL
  3. Save the project by going to File > Save Project As.

  4. Click SOAPSonar Header 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:

    1. Header Content-Length is 324771 bytes. This will vary based on the file you select.
    2. Content type is text/XML. This will change when MTOM is enabled.
    3. 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.

Enabling MTOM

In the section, we will enable MTOM for WebMatrix and the SOAPSonar test client. 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 in C:\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:

    WSE 3.0 Configurator
  4. Next, select the Messaging Tab and select “always” for the Server Mode MTOM Settings.

    WSE 3.0 Configurator
  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.

    SOAPSonar MTOM Setting
  9. Goto the ByteEcho_1 test case and click SOAPSonar MTOM Messages 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.
    SOAPSonar MTOM Messages

    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.

  10. 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.

Conclusions

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.

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

Contact Information

Website: www.crosschecknet.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here