I'm having trouble enabling GZIP compression on my C#.NET WCF web service and was hoping someone might know what I'm missing in my App.conf configuration file, or what extra is needed when making the call to start the web service in the code.
I've followed the link Applying GZIP Compression to WCF Services that points to the download of Microsoft example of adding GZIP but the example does not correlate with how I'm setting up my web service.
So my App.conf looks like
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="MyService.Service1">
<endpoint address="http://localhost:8080/webservice" binding="webHttpBinding" contract="MyServiceContract.IService"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<bindingElementExtensions>
<add name="gzipMessageEncoding" type="MyServiceHost.GZipMessageEncodingElement, MyServiceHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bindingElementExtensions>
</extensions>
<protocolMapping>
<add scheme="http" binding="customBinding" />
</protocolMapping>
<bindings>
<customBinding>
<binding>
<gzipMessageEncoding innerMessageEncoding="textMessageEncoding"/>
<httpTransport hostNameComparisonMode="StrongWildcard" manualAddressing="False" maxReceivedMessageSize="65536" authenticationScheme="Anonymous" bypassProxyOnLocal="False" realm="" useDefaultWebProxy="True"/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
I simply copied the config and GZIP classes from the MS example into my project and added my relevent web service configs. The code I'm using to start the windows service is:
WebServiceHost webserviceHost = new WebServiceHost(typeof(MyService.Service1));
webserviceHost.Open();
The webservice runs fine but Fiddler does not detect any response coming back with GZIP compressesion when making a call from a web browser. I also tried programmatically to setup and run the webservice with GZIP but failed miserably. Being green I'm not sure what else I need to configure, any advice is greatful
I've delved deeper into this and found out that since I'm running the webservice as a WebServiceHost object that it must be overriding the custom GZIP binding in the app.conf file with the WebHTTPBinding object that WebServiceHost defaults to, which means anything coming out of the web service will not be encoded. To get around this I figured that I would programmatically write the custom GZIP binding into the code
var serviceType = typeof(Service1);
var serviceUri = new Uri("http://localhost:8080/webservice");
var webserviceHost = new WebServiceHost(serviceType, serviceUri);
CustomBinding binding = new CustomBinding(new GZipMessageEncodingBindingElement(), new HttpTransportBindingElement());
var serviceEndPoint = webserviceHost.AddServiceEndpoint(typeof(IService), binding, "endpoint");
webserviceHost.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });
webserviceHost.Open();
The problem is that it won't allow a custom binding with the WebHttpBehavior. But if I remove the behavior then my REST webservice turns ugly and expects Stream objects as inputs in my contracts. I'm not sure how to configure behaviours so any help is greatful.