我有一个WCF服务,从数据库返回1000条记录到客户端。我有一个ASP。NET WCF客户端(我已经在asp.net web应用程序项目中添加了服务引用来使用WCF)。

当我运行客户端应用程序时,我得到以下消息:

传入消息的最大消息大小配额(65536)已为 超过了。如果需要增加配额,使用 MaxReceivedMessageSize属性 适当的绑定元素。

任何帮助吗?如何增加消息大小配额?


当前回答

对于HTTP:

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000" 
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="200" 
             maxArrayLength="200000000"
             maxBytesPerRead="4096"
             maxStringContentLength="200000000"
             maxNameTableCharCount="16384"/>
    </binding>
  </basicHttpBinding>
</bindings>

TCP:

<bindings>
  <netTcpBinding>
    <binding name="tcpBinding"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="200"
           maxArrayLength="200000000"
           maxStringContentLength="200000000"
           maxBytesPerRead="4096"
           maxNameTableCharCount="16384"/>
    </binding>
  </netTcpBinding>
</bindings>

重要的是:

如果你试图传递一个复杂的对象,这个对象有很多连接的对象(例如:一个树数据结构,一个有很多对象的列表……),无论你如何增加配额,通信都会失败。 在这种情况下,你必须增加包含对象的计数:

<behaviors>
  <serviceBehaviors>
    <behavior name="NewBehavior">
      ...
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

其他回答

如果您在使用WCF测试客户端时仍然得到这个错误消息,这是因为客户端有一个单独的MaxBufferSize设置。

要纠正这个问题:

右键单击树底部的Config File节点 选择用SvcConfigEditor编辑

将出现一个可编辑的设置列表,包括MaxBufferSize。

注意:自动生成的代理客户端也默认设置MaxBufferSize为65536。

我解决了问题…如下

    <bindings>
  <netTcpBinding>
    <binding name="ECMSBindingConfig" closeTimeout="00:10:00" openTimeout="00:10:00"
      sendTimeout="00:10:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
      maxReceivedMessageSize="2147483647" portSharingEnabled="true">
      <readerQuotas maxArrayLength="2147483647" maxNameTableCharCount="2147483647"
          maxStringContentLength="2147483647" maxDepth="2147483647"
          maxBytesPerRead="2147483647" />
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ECMSServiceBehavior">
      <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceTimeouts transactionTimeout="00:10:00" />
      <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="100"
        maxConcurrentInstances="100" />
    </behavior>
  </serviceBehaviors>
</behaviors>

从我的经验来看,另一件需要考虑的重要事情是…

我强烈建议不要最大化maxBufferPoolSize,因为在应用程序域(即应用程序池)回收之前,池中的缓冲区永远不会释放。

一段时间的高流量可能会导致大量内存被使用,并且永远不会释放。

详情如下:

https://stackoverflow.com/a/19953113/496676 http://andriybuday.com/2011/08/wcf-configuration-caused-memory-leaks.html

我找到了简单的方法

右键单击webconfig或app配置文件,然后单击EDIT WCF CONFIGURATION,然后进入bingdigs并选择您的服务和权限 边显示maxReciveMessageSize给出一个大的数字——

对于HTTP:

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000" 
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="200" 
             maxArrayLength="200000000"
             maxBytesPerRead="4096"
             maxStringContentLength="200000000"
             maxNameTableCharCount="16384"/>
    </binding>
  </basicHttpBinding>
</bindings>

TCP:

<bindings>
  <netTcpBinding>
    <binding name="tcpBinding"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="200"
           maxArrayLength="200000000"
           maxStringContentLength="200000000"
           maxBytesPerRead="4096"
           maxNameTableCharCount="16384"/>
    </binding>
  </netTcpBinding>
</bindings>

重要的是:

如果你试图传递一个复杂的对象,这个对象有很多连接的对象(例如:一个树数据结构,一个有很多对象的列表……),无论你如何增加配额,通信都会失败。 在这种情况下,你必须增加包含对象的计数:

<behaviors>
  <serviceBehaviors>
    <behavior name="NewBehavior">
      ...
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </serviceBehaviors>
</behaviors>