WCF学习笔记一

  在客户端调用WCF服务时,引发以下异常:

“/”应用程序中的服务器错误。


已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ServiceModel.QuotaExceededException: 已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。

  根据异常提示可知,这是由于WCF在传递数据时超过了它默认的最大值,这个时候就需要我们手动配置它的信息传递最大值(65536),代码如下:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_SubjectContract" />
        <binding name="BasicHttpBinding_AreaContract"  maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
        <binding name="BasicHttpBinding_SearchConfigContract" />
        <binding name="BasicHttpBinding_YearContract" />
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="MetadataExchangeHttpBinding_SubjectContract">
          <security mode="None" />
        </binding>
        <binding name="MetadataExchangeHttpBinding_AreaContract">
          <security mode="None" />
        </binding>
        <binding name="MetadataExchangeHttpBinding_SearchConfigContract">
          <security mode="None" />
        </binding>
        <binding name="MetadataExchangeHttpBinding_YearContract">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:1234/AreaService.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_AreaContract" contract="AreaService.AreaContract"
        name="BasicHttpBinding_AreaContract" />
      <endpoint address="http://localhost:1234/SearchConfigService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SearchConfigContract"
        contract="SearchConfigService.SearchConfigContract" name="BasicHttpBinding_SearchConfigContract" />
      <endpoint address="http://localhost:1234/SubjectService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SubjectContract"
        contract="SubjectService.SubjectContract" name="BasicHttpBinding_SubjectContract" />
      <endpoint address="http://localhost:1234/YearService.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_YearContract" contract="YearService.YearContract"
        name="BasicHttpBinding_YearContract" />
    </client>
  </system.serviceModel>

  找到对应服务的配置节点信息,添加红色部分代码即可。

代码说明:

      maxBufferPoolSize:获取或设置最大数量内存,在字节,可用于消息缓冲区管理器的分配在终结点接收消息配置了此绑定。

                 配置了此绑定的终结点所使用的缓冲池的最大大小(以字节为单位)。 默认值为 524288 个字节。 (简单理解为从通道接收消息的最大缓存数)

   maxReceivedMessageSize:获取或设置最大大小,在字节,在通道可接收配置了此绑定的消息。绑定可处理的最大消息大小(以字节为单位)。 默认值为 65,536 个字节。

                (简单理解为最大接收的消息大小) 

  如果读取的是XML数据,则可能需要添加以下红色部分代码:

<wsHttpBinding>
        <binding name="MetadataExchangeHttpBinding_SubjectContract">
          <security mode="None" />
        </binding>
        <binding name="MetadataExchangeHttpBinding_AreaContract">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
        <binding name="MetadataExchangeHttpBinding_SearchConfigContract">
          <security mode="None" />
        </binding>
        <binding name="MetadataExchangeHttpBinding_YearContract">
          <security mode="None" />
        </binding>
      </wsHttpBinding>

代码说明:    

    ReaderQuotas:获取或设置可由配置了此绑定的终结点处理的 SOAP 消息的复杂性约束。(定义可由采用绑定配置的终结点处理的 SOAP 消息的复杂性约束)

            说明:复杂性约束包括多个项,例如最大元素深度和消息中字符串内容的最大长度等。 这些约束可以抵御某种类型的拒绝服务 (DoS) 攻击,

               这些攻击试图利用消息复杂性来占用终结点处理资源。

    

<readerQuotas 
    maxArrayLength="Integer"
        maxBytesPerRead="Integer"
        maxDepth="Integer"
        maxNameTableCharCount="Integer"
    maxStringContentLength=="Integer" />

特性

 

特性

说明

maxArrayLength

一个正整数,指定 Windows Communication Foundation (WCF) 从客户端接收的数据的最大允许数组长度。 默认值为 16384。

maxBytesPerRead

一个正整数,指定每项读取操作返回的字节的允许最大数。 默认值为 4096。(最大每次读取长度)

maxDepth

一个正整数,指定每项读取操作的最大嵌套节点深度。 默认值为 32。(最大节点深度)

maxNameTableCharCount

一个正整数,指定表名称中允许的最大字符数。 默认值为 16384。(最大NameTableChar的数量)

maxStringContentLength

一个正整数,指定 XML 元素内容中允许包含的最大字符数。 默认值为 8192。(最大内容长度)

 

      

  

注意点:

    1.这些属性必须在客户端和服务上设置。 一旦在服务配置文件中设置,这些值将不自动传播到由 svcutil 工具或 Visual Studio 中的“添加服务引用”生成的客户端配置文件。 

       必须手动编辑生成的客户端配置文件并设置适当的值。

    2.复杂性约束可以抵御拒绝服务 (DOS) 攻击,这些攻击试图利用消息复杂性来占用终结点处理资源。 其他复杂性约束包括多个项,

       例如最大元素深度和消息中的字符串内容的最大长度。

    

Binding里的其它Max属性的意思:

  maxBufferSize="65536"      从通道接收消息的缓存大小.

  maxConnections="10"        最大连接数目.

参考文档:http://msdn.microsoft.com/zh-cn/library/system.servicemodel.wsdualhttpbinding.readerquotas(v=vs.110).aspx

       http://social.microsoft.com/Forums/zh-CN/73f31b97-bef5-47c6-b50e-d0d3140d8efb/wcf-bindingmax?forum=wcfzhchs

       http://msdn.microsoft.com/zh-cn/library/ms731325.aspx/html

       http://msdn.microsoft.com/zh-cn/beginner/ms731325(VS.90).aspx

       http://www.cnblogs.com/autumn/p/3502963.html

感谢您怀着耐心看完整篇博文!!!
如果文章有什么错误或不当之处,请您斧正!
您有任何意见或者建议,您可以给我发邮件,也可以在下面留言,我看到了会第一时间回复您的,谢谢! 

原文地址:https://www.cnblogs.com/djgs/p/3682506.html