How to get gzip compression working in WCF 4.5
Asked Answered
S

1

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

Schlessinger answered 28/3, 2013 at 11:2 Comment(13)
Some time time ago I had the same problem with WCF 4.0Arnie
dont think so. wcf 4.0 had no build in gzip support. it is a 4.5 feature.Schlessinger
Cool. I don't knew that.Arnie
i know how to write it myself like in 4.0, but i want the build in, which is for sure faster.Schlessinger
Unfortunately I stopped using WCF about a half year ago.Arnie
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."Gonion
"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> 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. "Gonion
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.Schlessinger
Have you checked out IIS level compression? #1735588Prodigy
yes, but my requirement is self hosting in a service. iis gzip works. thx anyway!Schlessinger
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.Temporary
thx. have the same thing as workaround. feel bit lame cannot use the built in stuff.Schlessinger
@Gonion please copy your comment as answer, it seems to be the correct answer, so that i can award the bounty.Schlessinger
G
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

Gonion answered 7/4, 2013 at 14:57 Comment(2)
Old post I know. But it's not working for me. Any idea why?Xenomorphic
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"?Quadrivium

© 2022 - 2024 — McMap. All rights reserved.