【问题标题】:WCF Service Binding taking default values instead of custom valuesWCF 服务绑定采用默认值而不是自定义值
【发布时间】:2010-01-13 12:48:45
【问题描述】:

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

<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>

通过在端点标签中设置“名称”属性来告诉配置使用我指定的绑定:

<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 配置,但为什么所有的值都是默认值,我不明白。有人可以提供有关如何自定义绑定值并使其反映在“客户端”部分的线索吗?

如果我在 app.config 中手动更改值,我会得到想要的效果,但是每次更新 web 引用时,VS 只会添加一个具有默认值的新绑定。

【问题讨论】:

    标签: wcf wcf-binding


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      你可以这样做:

      • 在单独的配置文件中定义您的绑定配置并在服务器上引用它 - 即将&lt;bindings&gt; 中的所有内容放入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”属性!

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

      【讨论】:

      • 感谢 marc_s。这是很有价值的信息,使配置更易读。另外:它省去了保持两个配置同步的麻烦。
      猜你喜欢
      • 2021-03-02
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 2020-01-29
      • 2010-11-08
      • 1970-01-01
      • 2011-05-14
      • 2012-08-05
      相关资源
      最近更新 更多