Wednesday, December 4, 2013

WCF Encodings & Bindings

Encoding is the process of transforming a message into a sequence of bytes. 
Decoding is the reverse process. 

Windows Communication Foundation (WCF) includes three types of encoding for SOAP messages: Text, Binary and Message Transmission Optimization Mechanism (MTOM).

I would explore major difference between the above in simple words:

-Text Encoding Use Base64 Encoding.It provides the widest interoperability compared to others.

- MTOM is basically a multi-part MIME message where binary data is base64-encoded and streamed into a separate section of the message.However, MTOM encoding creates multi-part messages only if the base64-encoded data size exceeds 1024 bytes. Otherwise, it simply keeps the base64 content inline and as a result brings down the overall message size (280 bytes less in this case) as there is no MIME header and additional section reference.This also provides significant interoperability because W3C has standardized MTOM.

-Binary encoding produces the smallest wire size compared to the other two encodings. Note that it also lacks the standard XML headers and it is a bit difficult to make out the actual content too unlike the other two. This should be considered when both the service and consumer are WCF-based.


In WCF, you specify how to transfer data across a network between endpoints by means of a binding, which is made up of a sequence of binding elements.

A binding includes -->
Optional protocol binding elements ( such as a security binding element or reliable messaging binding element)
Message encoding binding element
Transport binding element.


The message encoding binding element sits below the optional protocol binding elements and above the required transport binding element. On the outgoing side, a message encoder serializes the outgoing Message and passes it to the transport. On the incoming side, a message encoder receives the serialized form of a Message from the transport and passes it to the higher protocol layer, if present, or to the application, if not. 

WCF provides the following types of binding elements derived from the MessageEncodingBindingElement class that can provide for text, binary and Message Transmission Optimization Mechanism (MTOM) encoding:

  • TextMessageEncodingBindingElement: The most interoperable, but the least efficient encoder for XML messages. A Web service or Web service client can generally understand textual XML. However, transmitting large blocks of binary data as text is not efficient.
  • BinaryMessageEncodingBindingElement: Represents the binding element that specifies the character encoding and message versioning used for binary-based XML messages. This is most efficient of the encoding options, but the least interoperable, because it is only supported by WCF endpoints.
  • MTOMMessageEncodingBindingElement: Represents the binding element that specifies the character encoding and message versioning used for a message using a Message Transmission Optimization Mechanism (MTOM) encoding. MTOM is an efficient technology for transmitting binary data in WCF messages. The MTOM encoder attempts to balance between efficiency and interoperability. 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.

No comments: