WCF GZip 压缩请求/响应处理 [英] WCF GZip Compression Request/Response Processing

查看:31
本文介绍了WCF GZip 压缩请求/响应处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 WCF 客户端处理已被 IIS 压缩或压缩的服务器响应?

在 IIS 上,我已按照此处 关于如何使 IIS 6 gzip 由 .svc wcf 服务发出的所有响应(其中请求包含Accept-Encoding: gzip, deflate").

在客户端上,我已按照此处的说明进行操作此处 关于如何将此标头注入 Web 请求:Accept-Encoding: gzip,放气".

Fiddler2 显示响应是二进制的,而不是普通的旧 Xml.

客户端崩溃,出现一个异常,基本上说没有 Xml 标头,这当然是真的.

在我的 IClientMessageInspector 中,应用程序在 AfterReceiveReply 被调用之前崩溃了.

一些进一步的说明:

(1) 我无法更改 WCF 服务或客户端,因为它们是由第 3 方提供的.但是,如果这是正确的方向,我可以通过配置附加行为和/或消息检查器.

(2) 我不想只压缩/解压缩肥皂体,而是整个消息.

任何想法/解决方案?

* 已解决 *

编写 WCF 扩展来实现这些目标是不可能的.相反,我遵循了这个 CodeProject 文章,它提倡帮助类:

公共类 CompressibleHttpRequestCreator : IWebRequestCreate{公共 CompressibleHttpRequestCreator(){}WebRequest IWebRequestCreate.Create(Uri uri){HttpWebRequest httpWebRequest =Activator.CreateInstance(typeof(HttpWebRequest),BindingFlags.CreateInstance |BindingFlags.Public |BindingFlags.NonPublic |BindingFlags.Instance,null, new object[] { uri, null }, null) 作为 HttpWebRequest;如果(httpWebRequest == null){返回空;}httpWebRequest.AutomaticDecompression =DecompressionMethods.GZip |减压方法.放气;返回 httpWebRequest;}}

此外,对应用程序配置文件的补充:

<预><代码><配置><system.net><webRequestModules><remove prefix="http:"/><添加前缀="http:"type="Pajocomo.Net.CompressibleHttpRequestCreator, Pajocomo"/></webRequestModules></system.net></配置>

似乎正在发生的事情是,WCF 最终要求 system.net 中的某个工厂或其他深层次的工厂提供一个 HttpWebRequest 实例,而我们提供了将被要求创建所需实例的帮助程序.

在 WCF 客户端配置文件中,只需要一个简单的 basicHttpBinding,无需任何自定义扩展.

当应用程序运行时,客户端Http请求包含头Accept-Encoding: gzip, deflate",服务器返回一个gzip压缩的web响应,客户端透明地解压http响应,然后交给WCF.

当我尝试将此技术应用于 Web 服务时,我发现它不起作用.尽管辅助类的执行方式与 WCF 客户端使用时相同,但 http 请求不包含Accept-Encoding: ..."标头.

为了使其适用于 Web 服务,我必须编辑 Web 代理类,并添加此方法:

protected override System.Net.WebRequest GetWebRequest(Uri uri){System.Net.HttpWebRequest rq = (System.Net.HttpWebRequest)base.GetWebRequest(uri);rq.AutomaticDecompression = DecompressionMethods.GZip |减压方法.放气;返回 rq;}

请注意,应用程序配置文件中的 CompressibleHttpRequestCreator 和块是否存在无关紧要.对于 Web 服务,只有在 Web 服务代理中覆盖 GetWebRequest 才有效.

解决方案

感谢您的 WCF 提示!我们将在我的商店为服务启用 IIS 压缩,我希望您的解决方案能够奏效.通过使这项工作适用于 Web 服务" - 您是指老派的 SoapHttpProtocol 客户端吗?因为 SoapHttpProtocol 类有一个内置的 EnableDecompression 属性,它会自动处理压缩头和响应处理.

How do I get a WCF client to process server responses which have been GZipped or Deflated by IIS?

On IIS, I've followed the instructions here on how to make IIS 6 gzip all responses (where the request contained "Accept-Encoding: gzip, deflate") emitted by .svc wcf services.

On the client, I've followed the instructions here and here on how to inject this header into the web request: "Accept-Encoding: gzip, deflate".

Fiddler2 shows the response is binary and not plain old Xml.

The client crashes with an exception which basically says there's no Xml header, which ofcourse is true.

In my IClientMessageInspector, the app crashes before AfterReceiveReply is called.

Some further notes:

(1) I can't change the WCF service or client as they are supplied by a 3rd party. I can however attach behaviors and/or message inspectors via configuration if this is the right direction to take.

(2) I don't want to compress/uncompress just the soap body, but the entire message.

Any ideas/solutions?

* SOLVED *

It was not possible to write a WCF extension to achieve these goals. Instead I followed this CodeProject article which advocate a helper class:

public class CompressibleHttpRequestCreator : IWebRequestCreate
{
    public CompressibleHttpRequestCreator()
    {
    }

    WebRequest IWebRequestCreate.Create(Uri uri)
    {
        HttpWebRequest httpWebRequest = 
            Activator.CreateInstance(typeof(HttpWebRequest),
            BindingFlags.CreateInstance | BindingFlags.Public | 
            BindingFlags.NonPublic | BindingFlags.Instance,
            null, new object[] { uri, null }, null) as HttpWebRequest;

        if (httpWebRequest == null)
        {
            return null;
        }

        httpWebRequest.AutomaticDecompression =DecompressionMethods.GZip | 
            DecompressionMethods.Deflate;

        return httpWebRequest;
    }
} 

and also, an addition to the application configuration file:

<configuration>
  <system.net>
    <webRequestModules>
      <remove prefix="http:"/>
      <add prefix="http:" 
            type="Pajocomo.Net.CompressibleHttpRequestCreator, Pajocomo" />
    </webRequestModules>
  </system.net>
</configuration>

What seems to be happening is that WCF eventually asks some factory or other deep down in system.net to provide an HttpWebRequest instance, and we provide the helper that will be asked to create the required instance.

In the WCF client configuration file, a simple basicHttpBinding is all that is required, without the need for any custom extensions.

When the application runs, the client Http request contains the header "Accept-Encoding: gzip, deflate", the server returns a gzipped web response, and the client transparently decompresses the http response before handing it over to WCF.

When I tried to apply this technique to Web Services I found that it did NOT work. Although the helper class was executed in the same was as when used by the WCF client, the http request did not contain the "Accept-Encoding: ..." header.

To make this work for Web Services, I had to edit the Web Proxy class, and add this method:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
    System.Net.HttpWebRequest rq = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
    rq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    return rq;
}

Note that it did not matter whether the CompressibleHttpRequestCreator and block from the application config file were present or not. For web services, only overriding GetWebRequest in the Web Service Proxy worked.

解决方案

Thanks for your WCF tip! We're going to be enabling IIS compression for services at my shop, and I'm hoping your solution will work. By "To make this work for Web Services" - did you mean old school SoapHttpProtocol clients? Because the SoapHttpProtocol class has a built-in EnableDecompression property, which will automatically handle the Compression header and response handling.

这篇关于WCF GZip 压缩请求/响应处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆