Thursday, July 20, 2017

WCF MTOM MessageEncoding


In blog, we will learn about the Message Transmission Optimization Mechanism (MTOM) message encoding with a WSHttpBinding in WCF service and will demonstrates steps how to use the MTOM message encoding with C# example.


MTOM is an efficient technology for transmitting large binary data with SOAP in WCF messages. By default, the WSHttpBinding sends and received messages as normal text XML but transferring large chunk of data as normal XML text form, it is not better choice, if we choose the binary encoding, it only support to WCF-to- WCF communication and but exclude interoperability option for service.
MTOM encoder create a balance efficiency and interoperability and The MTOM encoding transmits most XML in textual form, but optimizes large blocks of binary data by transmitting them as-is, without conversion to text.

Here is pros and cons of message encoding:

·         Text Encoding :
o   Highly supports the interoperability
·         Binary Encoding:
o   High speed but only WCF –to- WCF communication
·         MTOM Encoding :
o   Message Transmission Optimization Protocol
o   Support both XML and binary

Here are steps how to use the MTOM message encoding in WCF Service

1.   Create Message Contract:

The method, which sends MTOM content, can only use [MessageContract] attribute decorated class or "primitive" types for receiving or sending data.

[MessageContract]
    public class OrderReport
    {
     [MessageHeader]
     public int OrderID { get; set; }

     [MessageBodyMember]
     public Byte OrderReportPDF { get; set; }
}

In [MessageContract] Class, the byte array properties must be decorated with a [MessageBodyMember] attribute and all the other properties of class must be decorated with [MessageHeader] attribute.


2.   Service Contract:

Here is a ServiceContract, which has a method GetOrderReportData to get Order Report Data based on input Order ID

namespace OrderWCF
{

    [ServiceContract]
    public interface IOrder
    {            
         [OperationContract]
             OrderReport GetOrderReportData(int OrderID);
     
    }
}


3.   WCF Binding Configuration:

Both client and server bindingConfiguation must have the messageEncoding attribute to Mtom

 <bindings>
  <wsHttpBinding>
    <binding name="WsHttpMtomBinding" messageEncoding="Mtom" />
  </wsHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="Behavior" name="OrderWCF.OrderService">
    <endpoint binding="wsHttpBinding" bindingConfiguration="WsHttpMtomBinding" contract="OrderWCF.IOrder">
  </service
</services>




1 comment:

zuas said...

This is a very useful productmelatonin gummies

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...