Web services having a greater interoperability to communicate messages across heterogeneous systems, however challenge is serializing the data into XML. Users may want to send images,videos, drawings, xml documents etc. together with SOAP message.
What is MTOM?
Message Transmission Optimization Mechanism (MTOM) is a mechanism of transferring transferring large amounts binary data as an attachment to SOAP message.
Typical SOAP message transmission shown in the below image
Image Source crosscheknet.com
WSE 3.0 supports Message Transmission Optimization Mechanism for transmitting large amounts of data to and from web services in earlier .NET FW versions now it is obsolete. We can write WCF service to use MTOM. In this post I am going to discuss uploading a file using WCF MTOM service and compares the payload of the message between text encoding and MTOM encoding.
1. Open the Visual Studio 2010 and create a WCF service
2. The service contract in the project looks as follows
3. The service Implementation class as below. Upload File method saves the supplied stream disk.
4. The Web.Config file settings for this service as follows
Create service client object and call the uploadfile method in client project.
Above code reading a file from disk and uploading the stream to service method. We can also compare the payload size how it looks when we used the normal text encoding and MTOM encoding.
The CompareMessageSize method code look as below
SizeOfMtomMessage method code below
Read file method code follows
The client application config looks as below
You only see the considerable difference when you are sending the large binary files with SOAP message using MTOM. When you send the small files using MTOM you would not see any optimization in transfer.
The point is MTOM is able to transmit the binary data as raw message with SOAP by saving the time which results the smaller messages.
|Share this post :|