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

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

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

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


当前回答

当我在web.config上使用这个设置时,我得到了这个错误

System.ServiceModel.ServiceActivationException

我像这样设置设置:

      <service name="idst.Controllers.wcf.Service_Talks">
    <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_TalksAspNetAjaxBehavior"
      binding="webHttpBinding" contract="idst.Controllers.wcf.Service_Talks" />
  </service>
  <service name="idst.Controllers.wcf.Service_Project">
    <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_ProjectAspNetAjaxBehavior"
      binding="basicHttpBinding" bindingConfiguration="" bindingName="largBasicHttp"
      contract="idst.Controllers.wcf.Service_Project" />
  </service>
</services>

<bindings>
<basicHttpBinding>
    <binding name="largBasicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="32"
             maxArrayLength="200000000"
             maxStringContentLength="200000000"/>
    </binding>
</basicHttpBinding>

其他回答

不要忘记将考虑执行入口点的app.config,而不是类库项目管理Web-Service调用(如果有的话)中的app.config。

例如,如果您在运行单元测试时得到错误,则需要在测试项目中设置适当的配置。

如果你正在动态创建你的WCF绑定,下面是使用的代码:

BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.MaxReceivedMessageSize = Int32.MaxValue;
httpBinding.MaxBufferSize = Int32.MaxValue;
// Commented next statement since it is not required
// httpBinding.MaxBufferPoolSize = Int32.MaxValue;

WCF测试客户端有自己的客户端配置。

运行测试客户端并滚动到底部。 如果双击Config File节点,您将看到XML表示。正如您所看到的,maxReceivedMessageSize是65536。

要编辑它,右键单击配置文件树节点并选择用SvcConfigEditor编辑。 当编辑器打开Bindings时,展开Bindings并双击自动生成的绑定。

您可以在这里编辑所有属性,包括maxReceivedMessageSize。当你完成后,点击文件-保存。

最后,当您回到WCF测试客户端窗口时,单击工具-选项。

注意:当启动服务时,取消选中Always generate配置。

当我在web.config上使用这个设置时,我得到了这个错误

System.ServiceModel.ServiceActivationException

我像这样设置设置:

      <service name="idst.Controllers.wcf.Service_Talks">
    <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_TalksAspNetAjaxBehavior"
      binding="webHttpBinding" contract="idst.Controllers.wcf.Service_Talks" />
  </service>
  <service name="idst.Controllers.wcf.Service_Project">
    <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_ProjectAspNetAjaxBehavior"
      binding="basicHttpBinding" bindingConfiguration="" bindingName="largBasicHttp"
      contract="idst.Controllers.wcf.Service_Project" />
  </service>
</services>

<bindings>
<basicHttpBinding>
    <binding name="largBasicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="32"
             maxArrayLength="200000000"
             maxStringContentLength="200000000"/>
    </binding>
</basicHttpBinding>

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