Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize WCF message in a binary way, and not as a SOAP Message

I have a client-server application, which communicates using WCF, and uses NetDataContractSerializer to serialize the objects graph.

Since a lot of data is transferred between the server and the client, I tried to decrease its size by fine tuning the size of the data members (e.g. changed int to short, long to int, etc.).

After finishing the tuning, I found out, that the amount of transferred data hasn't changed!
The problem is, that the NetDataContractSerializer serializes the objects graph to XML, so no matter what's the size of the data-member, the only thing that matters is the size of its value. For example, the value 10023 of a Int16 data member will be serialized as the string "10023" (0x3130303233), instead of just 10023 (0x2727).

I remember that in Remoting I could use the BinaryFormatter which serialized the values according to the type of the data member, but I don't know if it's possible to use it with WCF.

Does someone have a solution?

like image 625
Andy Avatar asked Aug 15 '09 20:08

Andy


People also ask

What is serialization in WCF service?

The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding . Net objects. This process is called serialization.

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.

Which namespace is used in WCF for data serialization?

DataContractSerializer as the Default By default WCF uses the DataContractSerializer class to serialize data types.

What is serialization and Deserialization in WCF?

For an introduction to data contracts, see Using Data Contracts. When deserializing XML, the serializer uses the XmlReader and XmlWriter classes. It also supports the XmlDictionaryReader and XmlDictionaryWriter classes to enable it to produce optimized XML in some cases, such as when using the WCF binary XML format.


2 Answers

WCF uses SOAP messages, but what kind of message encoding is used, is totally up to you.

Basically, out of the box, you have two: text encoding (text representation of XML message) or binary encoding. You can write your own message encoding, if you really must and need to.

Out of the box, the basicHttp and wsHttp bindings use text encoding - but you can change that if you want to. The netTcp binding (which is the clear preferred choice behind corporate firewalls) will use binary by default.

You can also define (just in config) your own "binary http" protocol, if you wish:

   <bindings>
      <customBinding>
        <binding name="BinaryHttpBinding">
          <binaryMessageEncoding />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>

and then use it in your service and client side config:

   <services>
      <service name="YourService">
        <endpoint
          address="http://localhost:8888/YourService/"
          binding="customBinding"
          bindingConfiguration="BinaryHttpBinding"
          contract="IYourService"
          name="YourService" />
      </service>
    </services>

Now you have a http-based transport protocol, which will encode your message in compact binary, for you to use and enjoy!

No additional coding or messy hacks or lots of manual XML serialization code needed - just plug it together and use it! Ah, the joy of WCF flexibility!

like image 73
marc_s Avatar answered Sep 27 '22 23:09

marc_s


First thought; have you enabled transport compression?

How complex is the data? If it is something that would work with the regular DataContractSerializer (i.e. a simple object tree), then protobuf-net may be of use. It is a very efficient binary serialization library with support for WCF via additional attributes on the service contract - for example:

[ServiceContract]
public interface IFoo
{
    [OperationContract, ProtoBehavior]
    Test3 Bar(Test1 value);
}

(the [ProtoBehaviour] is what swaps in the different serializer for this method)

However:

  • it needs to be able to identify a numeric tag for each property - either via extra attributes, or it can use the Order on a [DataMember(Order = x)] attribute
  • inheritance (if you are using it) requires extra attributes
  • it works best if you are using assembly sharing ("mex" doesn't love it...)

Nicely, it also works with MTOM, reducing the base-64 cost for larger messages.

like image 34
Marc Gravell Avatar answered Sep 28 '22 01:09

Marc Gravell