如何为wcf禁用gzip?

时间:2014-05-08 12:25:11

标签: wcf .net-4.5

我知道自动执行de / compression,我仍想禁用它。但是怎么样?我尝试添加textEncoding="Utf8Encoding"元素,但后来没有任何工作了。

我在标准的wcf服务中有这个:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />

    <diagnostics>
      <messageLogging logEntireMessage="true"
                                  logMalformedMessages="true"
                                  logMessagesAtServiceLevel="true"
                                  logMessagesAtTransportLevel="true"
                                  maxMessagesToLog="500"/>
    </diagnostics>
  </system.serviceModel>

这是我的wcf客户端:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <client>
      <endpoint address="http://localhost:5555/Service1.svc"
        binding="basicHttpBinding"
        contract="IService1"
        name="WebServiceEndpoint" />
    </client>
    <diagnostics>
      <messageLogging logEntireMessage="true"
                                  logMalformedMessages="true"
                                  logMessagesAtServiceLevel="true"
                                  logMessagesAtTransportLevel="true"
                                  maxMessagesToLog="500"/>
    </diagnostics>
  </system.serviceModel>

修改 我将此添加到服务但它不工作,我一直在获取HTTP / 1.1 415无法处理消息,因为内容类型&#39; text / xml; charset = UTF-8&#39;不是预期的类型&application; / soap + xml;字符集= UTF-8&#39;

<bindings>
  <customBinding>
    <binding name="test">
      <httpTransport decompressionEnabled="false"></httpTransport>
    </binding>
  </customBinding>
</bindings>
<protocolMapping>
    <add binding="customBinding" bindingConfiguration="test" scheme="http" />
</protocolMapping>  

2 个答案:

答案 0 :(得分:3)

客户端和服务器上的绑定必须完全相同。设置decompressionEnabled="false"时发生的关键是它停止将Accept-Encoding标题设置为“gzip,deflate”。好东西。

据我了解,该服务甚至不需要进行此配置,因为即使在客户端发送decompressionEnabled="false"时使用accept-encoding=gzip进行配置,它也会使用gzip压缩内容进行响应。

全职工作客户:

<bindings>
      <customBinding>
        <binding name="test">
          <textMessageEncoding messageVersion="Soap11" writeEncoding="utf-8"></textMessageEncoding>
          <httpTransport decompressionEnabled="false"></httpTransport>
        </binding>
      </customBinding>
    </bindings>
    <protocolMapping>
      <add binding="customBinding" bindingConfiguration="test" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <client>
      <endpoint address="http://localhost:5555/Service1.svc"
        binding="customBinding" bindingConfiguration="test"
        contract="IService1"
        name="WebServiceEndpoint" />
    </client>

全程服务:

<bindings>
      <customBinding>
        <binding name="test">
          <textMessageEncoding messageVersion="Soap11" writeEncoding="utf-8"></textMessageEncoding>
          <httpTransport decompressionEnabled="false"></httpTransport>
        </binding>
      </customBinding>
    </bindings>
    <protocolMapping>
        <add binding="customBinding" bindingConfiguration="test" scheme="http" />
    </protocolMapping>    

答案 1 :(得分:1)

您必须使用customBinding或通过代码更改DecompressionEnabled Property值创建<httpTransport decompressionEnabled="false" />