Although serialisation and encoding can be considered as closely related subjects in messaging, but from a design/implementation perspective they are completely independent processes running at different points in time as messages are sent between applications. In this post I will try to show how they work and highlight their differences.
Serialisation
Messages in WCF can be serialised using one of the three built-in serialisers:
- XmlSerializer
- DataContractSerializer
- NetDataContractSerializer
You can also implement your own serilisation algorithm if you want to.
The way a message is serialised has a design impact on the contract and so, the serialiser forms part of the service contract. Why? Because the serialisation algorithm specifies which elements are published, how they are published, etc. For example, consider those scenarios where the default behaviour of the serialiser does not meet your requirements so you have to implement ISerializable or IXMLSerializable. This is what we call a design impact.
You can specify the serialiser by decorating the service with DataContractFormatAttribute or XMLSerializerFormatAttribute. WCF does not provide an attribute for the NetDataContractSerializer because the WCF team wanted developers to follow the service orientation best practices by sharing the contract not the type (more on NetDataContractSerializer here).
But no matter which serialisation option (XMLSerailizer, DataContractSerializer or NetDataContractSerializer) you choose, the serialised message will be an XML infoset, although the resulting XML will not look like the same. So even if you use a .NET native binding like the NetTcpBinding and choose the DataContractSerializer, your message will still be serialised into XML.
If you want to know more about WCF serialisers, go and read Aaron Skonnard's article on MSDN.
Encoding
Once the message is serialised into an XML infoset, the specified binding will be applied before the message is sent to the destination. Each system-provided binding consists of a set of binding elements, each of which belongs to a binding layer that deals with a specific aspect of the messaging like security, session/ transaction management, transport and encoding (transport and encoding elements are mandatory).
The encoding binding element specifies how the XML infoset is encoded before it is passed to the transport layer. WCF provides Binary, Text and MTOM encodings and they are only available if they are supported for the transport protocol specified in the binding. For example in order to use the Binary encoding you need to use NetTcpBinding or NetNamedPipeBinding whereas Text and MTOM encodings are supported for the transport protocols based on web services like WSHttpBinding.
As you can see, unlike the serialisation, the encoding does not have an impact on the service contract and choosing the encoding type is not necessarily a design-time decision. That is why the encoding can be specified as a configuration element rather than an attribute on the service contract.