WCF服务绑定采用默认值而不是自定义值

时间:2010-01-13 12:48:46

标签: wcf wcf-binding

我已经构建了一个WCF服务的APi。在服务的web.config中,我已经指定了一个如下所示的自定义bindong:

<bindings>
  <wsHttpBinding>
    <binding name="FxBinding"
      maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647"
      closeTimeout="00:20:00"
      openTimeout="00:20:00"
      receiveTimeout="00:20:00"
      sendTimeout="00:20:00"
      messageEncoding="Mtom">
              <readerQuotas
      maxDepth="64"
      maxStringContentLength="2147483647"
      maxArrayLength="2147483647"
      maxBytesPerRead="4096"
      maxNameTableCharCount="16384" />
      <security mode="None"></security>
    </binding>
  </wsHttpBinding>
</bindings>

告诉配置使用我在端点标记中设置“name”属性指定的绑定:

<services>
  <service name="Dba.Admanager.Api.ListingServiceContract" behaviorConfiguration="Dba.Admanager.Api.ListingServiceContractBehavior">
    <endpoint address="" binding="wsHttpBinding" name="FxBinding" contract="Dba.Admanager.ApplicationController.Api.IListingServiceContract">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>

请参阅。当我在VS2008中创建对服务的Web引用时,会生成app.config。 在这个app.config中使用的绑定应该是上面指定的绑定。但是在app.config中是一个默认绑定,例如“maxArrayLength”中的默认值。

<bindings>
        <wsHttpBinding>
            <binding name="FxBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
                transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="">
                        <extendedProtectionPolicy policyEnforcement="Never" />
                    </transport>
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://l-aar-00424012.corp.ebay.com/Admanager.Api/ListingServiceContract.svc"
            binding="wsHttpBinding" bindingConfiguration="FxBinding" contract="WCFListingServiceContract.IListingServiceContract"
            name="FxBinding">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>

我可以看到客户端正在使用FxBinding配置,但为什么地球上所有值都是默认值,我不明白。 anubody能否提供关于如何自定义绑定值并使其反映在“客户端”部分的线索?

如果我更改app.config中的值manualle,我会得到所需的效果,但每次更新web引用时,VS只会添加一个带有默认值的新绑定。

2 个答案:

答案 0 :(得分:4)

maxArrayLength之类的信息不是WSDL的一部分,因此客户端无法推断它并只采用默认值。

答案 1 :(得分:2)

正如Darin已经提到的那样 - 由于这些绑定参数不是服务和客户端之间可互操作元数据的一部分,因此当您创建新的客户端代理时,客户端无法自动获取它们。

你能做的是:

  • 在单独的配置文件中定义绑定配置,并在服务器上引用它 - 即将<bindings>内的所有内容放入bindings.config,然后从服务器端web.config或app引用它的.config:

    <system.serviceModel>
        <bindings configSource="bindings.config" />
    </system.serviceModel>
    
  • 然后将bindings.config复制到您的客户端并在app.config中执行相同的操作。

注意:是的,Visual Studio会抱怨没有定义“configSource”和所有 - 但这只是配置文件的VS intellisense中的一个缺陷。相信我 - 它的工作原理 - 我每天都在生产系统中使用它! .NET中的每个配置部分都有一个“configSource”属性!

使用此方法,您可以创建一个包含系统绑定配置的文件,并且可以在服务器端和客户端使用它。