20

WCF 4.5 supports GZIP without third party libraries or handwritten extensions. I got it working via TCP Binding, but cannot find a way to get it working via HTTP Binding. my wcf - Service is self hosted in a windows service.

Addon: i am not allowed to use IIS; i can't switch to any WCF replacement.

this works with gzip:

binding="customBinding" bindingConfiguration="tcpCompressionBinding" name="tcp" 

and this is what i currently use for http:

binding="basicHttpBinding" bindingConfiguration="httpBinding" name="http"

The documentation does not really help me: http://msdn.microsoft.com/en-us/library/dd456789.aspx.

But, according to this it should work:

Beginning with WCF 4.5 the WCF binary encoder adds support for compression. The type of compression is configured with the CompressionFormat property. Both the client and the service must configure the CompressionFormat property. Compression will work for HTTP, HTTPS, and TCP protocols. If a client specifies to use compression but the service does not support it a protocol exception is thrown indicating a protocol mismatch. For more information, see Choosing a Message Encoder

NickD
  • 2,672
  • 7
  • 37
  • 58
  • Some time time ago I had the same problem with WCF 4.0 – rekire Mar 28 '13 at 20:44
  • 1
    dont think so. wcf 4.0 had no build in gzip support. it is a 4.5 feature. – NickD Mar 28 '13 at 20:45
  • Cool. I don't knew that. – rekire Mar 28 '13 at 20:47
  • i know how to write it myself like in 4.0, but i want the build in, which is for sure faster. – NickD Mar 28 '13 at 20:48
  • Unfortunately I stopped using WCF about a half year ago. – rekire Mar 28 '13 at 20:54
  • 1
    I guess you already read this in the documentation but just in case you missed it: "Beginning with WCF 4.5 the WCF binary encoder adds support for compression. This enables you to use the gzip/deflate algorithm for sending compressed messages from a WCF client and also respond with compressed messages from a self-hosted WCF service. This feature enables compression on both the HTTP and TCP transports. An IIS hosted WCF service can always be enabled for sending compressed responses by configuring the IIS host server. The type of compression is configured with the CompressionFormat property." – jpw Mar 30 '13 at 12:22
  • 1
    "Since this property is only exposed on the binaryMessageEncodingBindingElement, you will need to create a custom binding like the following to use this feature: Both the client and the service need to agree to send and receive compressed messages and therefore the compressionFormat property must be configured on the binaryMessageEncoding element on both client and service. " – jpw Mar 30 '13 at 12:22
  • yes, have read this. there seems to be no httpcompression binding, and in the normal i cannot find the property. tcp with compression works fine. – NickD Mar 30 '13 at 12:26
  • 2
    Have you checked out IIS level compression? http://stackoverflow.com/questions/1735088/how-to-enable-iis-compression-for-wcf-services – lcryder Apr 01 '13 at 18:18
  • yes, but my requirement is self hosting in a service. iis gzip works. thx anyway! – NickD Apr 01 '13 at 18:48
  • If you get totally stuck, I have some custom code that plugs into the WCF infrastructure that does the zipping and unzipping for you. I was using it with WCF 3.5 so I can't see any reason why it won't work with 4.5. – Rocklan Apr 03 '13 at 05:20
  • thx. have the same thing as workaround. feel bit lame cannot use the built in stuff. – NickD Apr 03 '13 at 07:55
  • @jpw please copy your comment as answer, it seems to be the correct answer, so that i can award the bounty. – NickD Apr 07 '13 at 09:39

1 Answers1

23

As per request I copied my comment as answer:

"Since this property is only exposed on the binaryMessageEncodingBindingElement, you will need to create a custom binding like the following to use this feature:

<customBinding>
  <binding name="BinaryCompressionBinding"> 
    <binaryMessageEncoding compressionFormat="GZip"/> 
    <httpTransport /> 
  </binding>
</customBinding> 

and receive compressed messages and therefore the compressionFormat property must be configured on the binaryMessageEncoding element on both client and service. "Both the client and the service need to agree to send

jpw
  • 44,361
  • 6
  • 66
  • 86
  • Old post I know. But it's not working for me. Any idea why? – Ahmed Khalaf Oct 17 '16 at 13:25
  • 1
    how to do this in code? do I need to create a custom binding or creating `var binding = new NetTcpBinding()` and then `var elements = binding.CreateBindingElements()` and then `elements.Add(new BinayMessageEncodingBindingElement{CompressionFormat=Gzip})`? does this **change** the binding or the `elements` are "lost"? – JobaDiniz Apr 09 '20 at 17:37