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

10

我遇到了以下错误:"入站消息的最大消息大小配额 (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>

我不明白为什么我的绑定配置没有被应用?!WCF测试客户端已设置为始终重新生成配置。我还尝试在消费前端应用程序中更新服务引用,但它也没有获取更新的绑定配置。非常感谢您的任何建议。谢谢!


我看你的客户正在获取你的绑定... - Brian Driscoll
但它没有我的更大的maxBufferPoolSize和maxReceivedMessageSize值... - MisterIsaak
2个回答

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>
那样,您可以只配置一次绑定,然后在两个地方使用。

哇,感谢您的详细解释!因此,由于客户端不会使用服务器的绑定配置,我需要手动为它们单独配置设置。我假设当您首次引用服务时,客户端必须看到绑定类型并设置所需的默认值。再次感谢! - MisterIsaak

2

maxBufferPoolSize和maxReceivedMessageSize并不向客户端公开,只有服务器知道它们的大小。客户端使用的大小是默认值,您可以将它们更改为任何大小。如果您经常重新生成它,则显然会出现问题,但我认为没有太多替代方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接