如何将gzip MessageEncoding bindingElementExtension添加到现有的WCF / WSDL应用程序?

时间:2014-11-12 12:01:08

标签: c# .net wcf wsdl

我正在尝试将gzip压缩消息编码器从MS WCF samples包中删除到现有的WCF / WSDL应用程序(使用basicHttpBinding),但我遇到了应用配置问题。

我将GZipEncoder项目从示例添加到我的解决方案(包含GZipMessageEncoderFactory,GZipMessageEncodingBindingElement和GZipMessageEncodingBindingElementImporter),然后将扩展名添加到Web.config的serviceModel/extensions中(完整的最终web.config位于我的底部)问题):

<bindingElementExtensions>
  <add name="gzipMessageEncoding" type="Microsoft.Samples.GZipEncoder.GZipMessageEncodingElement, GZipEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</bindingElementExtensions>

..将gzipMessageEncoding添加到bindings/basicHttpBinding/binding部分:

<gzipMessageEncoding innerMessageEncoding="textMessageEncoding" />
解析器无法识别

gzipMessageEncoding,因此我将basicHttpBinding替换为serviceModel/bindings中的自定义:

<customBinding>
  <binding name="customBinding" closeTimeout="00:15:00" openTimeout="00:15:00" receiveTimeout="00:15:00" sendTimeout="00:15:00">
    <gzipMessageEncoding innerMessageEncoding="textMessageEncoding"/>
    <httpTransport hostNameComparisonMode="StrongWildcard" manualAddressing="False" maxReceivedMessageSize="2147483647" 
     authenticationScheme="Anonymous" bypassProxyOnLocal="False" realm="" useDefaultWebProxy="True" transferMode="Buffered"/>
  </binding>
</customBinding>

serviceModel

<protocolMapping>
  <add scheme="http" binding="customBinding" />
</protocolMapping>

然后在我的端点中命名为customBinding:

<service behaviorConfiguration="KiProjectsSyncServiceBehavior" name="KiProjectsSyncService">
  <endpoint address="" binding="customBinding" bindingConfiguration="KiProjectsSyncService" contract="IKiProjectsSyncService"/>

但是当我浏览服务时出现错误:

  

system.serviceModel / bindings / customBinding上的绑定没有   有一个名为'KiProjectsSyncService'的配置绑定。这是个   bindingConfiguration的值无效。

我只想将gzip附加到basicHttp或其他一些简单的文本绑定。样本似乎是这样做的,但我看不出我偏离它的例子。理解这个配置会很棒,因为这个功能将来需要适应各种环境,我决定反对REST的原因之一就是这种架构的灵活性。

仅供参考:我希望双向压缩,我相信在IIS中打开压缩只会压缩下载。我的应用程序是两个IIS服务器ASP应用程序相互通信。我通过接口传输DataContract和MessageContract类型(后者用于在两个方向上传输文件,尽管实际上它们是在我使用BasicHttp时被缓冲的)。它运作良好 - 只是努力增加压缩。我还更改了客户端web.config(bindingElementExtensions,endPoint bindingConfig和binding,policyImporters和customBinding),但是在尝试浏览我的.svc服务时遇到这些问题,而不涉及客户端。

完整的web.config:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="KiSuiteConnectionString" connectionString="Data Source=xxxx;Initial Catalog=xxxx;Persist Security Info=True;User ID=xxxx;Password=xxxx" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Enabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime executionTimeout="999999" maxRequestLength="2147483647" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
      </requestFiltering>
    </security>
  </system.webServer>
  <system.serviceModel>
    <extensions>
      <bindingElementExtensions>
        <add name="gzipMessageEncoding" type="Microsoft.Samples.GZipEncoder.GZipMessageEncodingElement, GZipEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </bindingElementExtensions>
    </extensions>
    <protocolMapping>
      <add scheme="http" binding="customBinding" />
    </protocolMapping>
    <bindings>
      <customBinding>
        <binding name="customBinding" closeTimeout="00:15:00" openTimeout="00:15:00" receiveTimeout="00:15:00" sendTimeout="00:15:00">
          <gzipMessageEncoding innerMessageEncoding="textMessageEncoding"/>
          <httpTransport hostNameComparisonMode="StrongWildcard" manualAddressing="False" maxReceivedMessageSize="2147483647"
                         authenticationScheme="Anonymous" bypassProxyOnLocal="False" realm="" useDefaultWebProxy="True" transferMode="Buffered"/>
        </binding>
      </customBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="KiProjectsSyncServiceBehavior" name="KiProjectsSyncService">
        <endpoint address="" binding="customBinding" bindingConfiguration="KiProjectsSyncService" contract="IKiProjectsSyncService"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="KiProjectsSyncServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:0)

我错误的是endpoint bindingConfiguration必须设置为与绑定相同的值(在我的情况下,customBinding),即:

<services>
  <service behaviorConfiguration="KiProjectsSyncServiceBehavior" name="KiProjectsSyncService">
    <endpoint address="" binding="customBinding" bindingConfiguration="customBinding" contract="Kimetrica.Sync.IKiProjectsSyncService"/>
  </service>
</services>

想象 bindingConfiguration设置可用于将新的自定义插入到WCF框架中。然后在客户端web.config中我必须添加以下内容:

  <system.serviceModel>
    <extensions>
      <bindingElementExtensions>
        <add name="gzipMessageEncoding" type="Kimetrica.Sync.GZipEncoder.GZipMessageEncodingElement, GZipEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </bindingElementExtensions>
    </extensions>
    <bindings>
      <customBinding>
        <binding name="customBinding" closeTimeout="00:15:00" openTimeout="00:15:00"
          receiveTimeout="00:15:00" sendTimeout="00:15:00">
          <gzipMessageEncoding innerMessageEncoding="textMessageEncoding" />
          <httpTransport manualAddressing="false" authenticationScheme="Anonymous"
            bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            proxyAuthenticationScheme="Anonymous" realm="" useDefaultWebProxy="true" maxReceivedMessageSize="2147483647" transferMode="Buffered" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:1062/WcfTestWebSite/KiProjectsSyncService.svc"
                binding="customBinding" bindingConfiguration="customBinding"
                contract="KiProjectsSyncServiceReference.IKiProjectsSyncService" name="KiProjectsSyncService" />
      <metadata>
        <policyImporters>
          <extension type="Microsoft.Samples.GZipEncoder.GZipMessageEncodingBindingElementImporter, GZipEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </policyImporters>
      </metadata>
    </client>
  </system.serviceModel>

这成功地添加了gzip压缩,并将包含FileStreams的DataContracts和MessageContracts(包括超过默认的64kb限制),IIS ASP应用程序发送到IIS ASP应用程序。