为什么我的WCF服务没有加载我的Binding配置?

时间:2010-10-27 17:16:47

标签: wcf wcf-binding wcf-client wcftestclient

我遇到以下错误的问题:“已超出传入邮件的最大邮件大小限额(65536)。要增加配额,请在相应的绑定元素上使用MaxReceivedMessageSize属性。

所以我做了一些研究,发现我需要增加缓冲区和消息大小,这是我的WCF服务配置文件:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
          <binding name="default" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"/>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WCF.Service.Service">
        <endpoint address="ws" name="ws" bindingConfiguration="default" binding="wsHttpBinding" contract="WCF.Service.Contracts.IService" />
        <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

当我在WCF测试客户端中运行该服务并查看生成的客户端配置文件时,拥有我的绑定:

<configuration>
<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="ws" 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="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:37444/Service.svc/ws" binding="wsHttpBinding"
            bindingConfiguration="ws" contract="IService" name="ws">
            <identity>
                <userPrincipalName value="username@domain" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

我迷失了为什么我的绑定配置 NOT 被应用了!? WCF测试客户端设置为始终重新生成配置。我也尝试在消费前端应用程序中更新服务引用,但也获得更新的绑定配置。任何建议将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:11)

WCF 从您的服务器导入所有设置。也没有开关打开它。虽然在许多情况下它可能有意义,但是将服务器端的所有设置复制到客户端并不总是一个好主意。

因此,在您的情况下,您需要做的是将绑定配置添加到客户端代理,并从客户端端点引用它。

如果你控制电线的两端,你可以稍微简化你的工作:将绑定配置外部化为一个单独的文件并引用它。

所以创建一个包含以下内容的文件bindings.config

<?xml version="1.0" ?>
<bindings>
    <wsHttpBinding>
        <binding name="default" 
                 maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647"/>
  </wsHttpBinding>
</bindings>

然后您可以将该文件复制到服务器和客户端项目中,并从服务和客户端配置中引用它:

<system.serviceModel>
    <bindings configSource="bindings.config" />
    <services>
      <service name="WCF.Service.Service">
        <endpoint address="ws" name="ws" bindingConfiguration="default" binding="wsHttpBinding" contract="WCF.Service.Contracts.IService" />
        <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
      </service>
    </services>

并在您的客户端:

<system.serviceModel>
    <bindings configSource="bindings.config" />
    <client>
        <endpoint  name="ws"
            address="http://localhost:37444/Service.svc/ws" 
            binding="wsHttpBinding"
            bindingConfiguration="default" 
            contract="IService">
            <identity>
                <userPrincipalName value="username@domain" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

这样,您可以对绑定进行一次配置,并在两个地方使用。

答案 1 :(得分:2)

maxBufferPoolSize和maxReceivedMessageSize不会向客户端公开,只有服务器知道它们。客户端使用的大小是默认值,只需将它们更改为您想要的任何大小。显然这是有问题的,如果你正在重新生成它,但我不认为有很多替代方案。

相关问题