且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ProtocolException : 已超过传入消息(65536)的最大消息大小配额。

更新时间:2022-09-16 20:13:42

SilverLight调用WCF,提交的是一个List<Linq2SqlEntity>;当List中数据量不大的时候,不会报错;当数据量稍微大一点儿,就会出现这个错误。发生了 System.ServiceModel.ProtocolException
  Message=已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。
  Source=System.ServiceModel
  StackTrace:
  在 System.ServiceModel.Channels.HttpInput.ThrowHttpProtocolException(String message, HttpStatusCode statusCode, String statusDescription)

 

    费了牛劲在网上搜了一通,有的说是要修改behavior的maxItemsInObjectGraph,有的说要修改binding的maxBufferSize/maxReceivedMessageSize,有的说要增加readerQuotas,有的是endpoint没有与自定义binding配置节关联起来,有的说要客户端和服务器端都要改(我的应用中,接受数据的是服务器端,客户端负责提交,所以应该不关客户端的事儿)。。。然后把server端的配置文件调整成下面这样:

   1: <system.serviceModel>
   2:   <behaviors>
   3:     <serviceBehaviors>
   4:       <behavior name="MessageHeaderOperationBehaviourAuthenticationBehavior">
   5:         <dataContractSerializer />
   6:         <serviceMetadata httpGetEnabled="true"/>
   7:         <serviceDebug includeExceptionDetailInFaults="true"/>
   8:       </behavior>
   9:     </serviceBehaviors>
  10:   </behaviors>
  11:   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  12:   <services>
  13:     <service behaviorConfiguration="MessageHeaderOperationBehaviourAuthenticationBehavior"
  14:              name="MessageHeaderOperationBehaviourAuthentication">
  15:       <endpoint address="" binding="basicHttpBinding" ="" contract="DyeService">
  16:       </endpoint>
  17:     </service>
  18:   </services>
  19:   <bindings>
  20:     <basicHttpBinding>
  21:       <binding name=""  maxBufferSize="2147483647"
  22:                maxBufferPoolSize="21474836471" 
  23:         < maxDepth="2147483647" maxStringContentLength="2147483647"
  24:                       maxArrayLength="2147483647" maxBytesPerRead="2147483647"
  25:                       maxNameTableCharCount="2147483647" />
  26:         <security mode="None" />
  27:       </binding>
  28:     </basicHttpBinding>
  29:   </bindings>
  30: </system.serviceModel>

       但是问题依旧。最后在***找到了正解:WCF Error - unexpected response: (400) Bad Request.,里面链接到了另一篇帖子:Cannot get the MaxReceivedMessageSize higher than (65536)

 

      解决办法就是:配置文件中,service 的name属性必须与服务的类型全名称保持一致,才能应用上自定义的配置节。

      按着这个方法,调整之后的配置文件如下所示(删掉了其他无关的maxItemsInObjectGraph、readerQuotas等):

   1:  
   2:   <system.serviceModel>
   3:     <behaviors>
   4:       <serviceBehaviors>
   5:         <behavior name="MessageHeaderOperationBehaviourAuthenticationBehavior">
   6:           <serviceMetadata httpGetEnabled="true"/>
   7:           <serviceDebug includeExceptionDetailInFaults="true"/>
   8:         </behavior>
   9:       </serviceBehaviors>
  10:     </behaviors>
  11:     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  12:     <services>
  13:       <service behaviorConfiguration="MessageHeaderOperationBehaviourAuthenticationBehavior"
  14:                
  15:         <endpoint address="" binding="basicHttpBinding"  >
  16:         </endpoint>
  17:       </service>
  18:     </services>
  19:     <bindings>
  20:       <basicHttpBinding>
  21:         <binding   maxBufferSize="2147483647"
  22:                  maxBufferPoolSize="21474836471" maxReceivedMessageSize="2147483647">
  23:         </binding>
  24:       </basicHttpBinding>
  25:     </bindings>
  26:   </system.serviceModel>

 

本文转自Silent Void博客园博客,原文链接:http://www.cnblogs.com/happyhippy/archive/2011/07/02/2096482.html,如需转载请自行联系原作者