代码之家  ›  专栏  ›  技术社区  ›  esbenr

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

  •  2
  • esbenr  · 技术社区  · 15 年前

    我已经构建了一个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>
    

    告诉配置使用我通过在端点标记中设置“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中手动更改值,我会得到所需的效果,但是每次更新WebReference时,vs都会添加一个带有默认值的新绑定。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Darin Dimitrov    15 年前

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

        2
  •  2
  •   marc_s    15 年前

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

    你可以做的是:

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

      <system.serviceModel>
          <bindings configSource="bindings.config" />
      </system.serviceModel>
      
    • 然后复制 绑定.CONFIG 到你的客户端,在app.config中做同样的事情。

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

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