5

I am building a custom encoder that compresses WCF responses. It is based on the Gzip encoder in Microsoft's WCF samples and this blog post:

http://frenk.wordpress.com/2009/12/04/gzip-compression-wcfsilverlight/

I've got it all working, but now I would like to apply the compression only if the reply is beyond a certain size, but I am not sure how to retrieve the total size of the actual message from the encoder level.

I would need to get the message size at both the WriteMessage(...) method in the EncoderFactory, so I know whether to compress the message) and at the BeforeSendReply(...) method in the DispatchMessageInspector so that I can add the "gzip" ContentEncoding header to the response. Requests are always small and not compressed, so I don't need to worry about that.

Any help appreciated.

Jon.

user250837
  • 81
  • 5

2 Answers2

3

I think you would do this in two stages. First, write a custom MessageEncoder that encodes the message to a byte[] just normal. Once you have the encoded byte-array (and this can be any message encoding format... Xml, Json, binary, whatever) you can examine the byte-array size and determine whether you want to create another compressed byte array.

Several resources you may find useful:

Simon Gillbee
  • 3,932
  • 4
  • 35
  • 48
0

You can try calculating it based on reply.ToString.Length() and message.ToString.Length()

Tanner
  • 22,205
  • 9
  • 65
  • 83
  • Thanks for answering, but both the reply and the message are reference objects at that point (i'm using an custom inner encoder), so I can't get the length that way. If I drill down into the object, I can get the actual content, which is still a reference object. I'm not sure how to get a hold of the serialized raw data before it is sent. – user250837 Aug 17 '10 at 11:21