views:

2938

answers:

3

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?

+2  A: 

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.

Marc Gravell
A: 

Why are you using the NetDataContractSerializer manually? Why not just have your service return your object graph? WCF will do the serialization for you.

In particular, if you will use the netTcpBinding, you'll find that your data is serialized in binary.

Also, you'll probably get better performance if you stick to the DataContractSerializer. NetDataContractSerializer also serializes type metadata that you probably don't need.

John Saunders
If I will just use netTcpBinding, how will WCF serialize the objects graph? what kind of serializer it will use? NetDataContractSerializer serializes to SOAP too, so if it'll work, then it must be using something else...
Andy
You are making bad assumptions. From http://msdn.microsoft.com/en-us/library/ms731073.aspx: "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."
John Saunders
I know that it can be optimized, but it still gonna be XML. I don't want it to be XML, when a number written to XML, it becomes text, and then it's written to the stream as text (like in the example I provided in my question)
Andy
What makes you think that a number will be turned into text? Have you ever seen the binary xml format?
John Saunders
+6  A: 

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 confing) 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! :-)

Marc

marc_s