Compression of requests of .NET 4.5 and WCF

We have a web application hosted in a third-party hosting environment. The server application provides some WCF RESTFUL services for our iPad applications.

WCF services are .svc-less and are registered in the Glonbal.asax file. Example:

RouteTable.Routes.Add("service name", new ServiceRoute("url", new WebServiceHostFactory(), routingServiceContract)); 

Since we need to transfer some massive data from our iPad applications to the server application, some of them will be compressed by GZIP before sending to the WCF service. And since the server application was originally built on the basis of .NET 3.5, a piece of code like this was responsible for decompressing compressed requests:

 public void Application_BeginRequest(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request.Headers["Content-Encoding"])) { if (Request.Headers["Content-Encoding"].ToLower().Contains("gzip")) Request.Filter = new GZipStream(Request.Filter, CompressionMode.Decompress); if (Request.Headers["Content-Encoding"].ToLower().Contains("deflate")) Request.Filter = new DeflateStream(Request.Filter, CompressionMode.Decompress); } } 

This worked until our hosting provider installed .NET 4.5 on their server. Then the compressed JSON requests started to crash (we get http 400 or sometimes 500 errors). After such a big investigation, it turned out that the code and the web.config file were fine, because the WCF service works fine on a server that does not have .NET 4.5 installed.

I even commented on the code above and posted it on the same server, but it did not work again. I thought the request was decoded twice!

Now I am wondering how can I use the built-in compression function of WCF 4.5 and make this website work? I prefer to get rid of custom C # code and just use the WCF 4.5 compression function.

ps the web service is in ASP.NET compatibility mode.

The pps web service works fine if we do not compress the HTTP request.

+4
source share
1 answer

I looked at your configuration file and the Application_BeginRequest method. I copied your configuration file on my machine with .net 4.0 and confirmed that it works fine. I could also see that the same settings did not work on a computer with .net 4.5. Based on my observation, your problem is not related to the compression / decompression code that you have in Application_BeginRequest. But you are faced with the same problem that I described in this article [post] [1]

[1]: WCF - Conflicting endpoints after installing .Net 4.5 Mail. In this post, I also published a paper on this issue. The work around is to define the REST endpoint explicitly. Following the same work, I saw that your project began to work. Exactly all I needed to do for your project to work was to add these two lines to the config and explicitly configure the service endpoint.

  <services> <service name="<YourNamespace>.TestService"> <endpoint address="" binding="webHttpBinding" contract="<YourNamespace>.TestService" behaviorConfiguration="RESTEndpointBehavior"/> </service> </services> 

Can you check if this solves your problem?

0
source

All Articles