【问题标题】:Maximum Array Length Quota Exception in WCFWCF 中的最大数组长度配额异常
【发布时间】:2011-05-05 11:08:34
【问题描述】:

我正在编写一个 WCF 服务来上传文件,但是当字节数组的元素超过 16384 个时它会引发异常。

这是异常详情:

格式化程序抛出异常,而 试图反序列化消息: 反序列化请求正文时出错 操作信息 '创建文档'。最大数组 已超出长度配额 (16384) 在读取 XML 数据时。这个配额可以 通过改变来增加 MaxArrayLength 属性 使用的 XmlDictionaryReaderQuotas 对象 创建 XML 阅读器时。 1号线, 位置 22862。

客户端和服务器的配置都将最大数组长度配额设置为 2147483647。

客户端配置:

<system.serviceModel>
  <bindings>
   <basicHttpBinding>
    <binding name="BasicHttpBinding_IDocumentLibraryService" closeTimeout="00:01:00"
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
     allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
     maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
     useDefaultWebProxy="true">
     <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
      maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
     <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None"
       realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
     </security>
    </binding>
   </basicHttpBinding>
  </bindings>
  <client>
   <endpoint address="http://localhost:50764/DocumentLibraryService.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDocumentLibraryService"
    contract="DocumentLibrary.IDocumentLibraryService" name="BasicHttpBinding_IDocumentLibraryService" />
  </client>

服务器配置:

<bindings>

            <basicHttpBinding>
                <binding name="BasicHttpBinding_IDocumentLibraryService" closeTimeout="00:01:00"
                 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                 allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                 messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                 useDefaultWebProxy="true">
                    <readerQuotas maxDepth="2147483647"
                maxStringContentLength="2147483647"
                maxArrayLength="2147483647"
                maxBytesPerRead="2147483647"
                maxNameTableCharCount="2147483647" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                         realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <services>

            <service name="BasicHttpBinding_IDocumentLibraryService">

                <clear />
                <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" />
                <endpoint binding="basicHttpBinding" name="DocumentLibraryService" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" address=""
                          bindingConfiguration="BasicHttpBinding_IDocumentLibraryService"/>
            </service>
        </services>

【问题讨论】:

    标签: wcf readerquotas


    【解决方案1】:

    我需要做的就是将 web.config 文件中的服务名称更改为带有命名空间的完整服务名称:

    <service name="SampleNameSpace.DocumentLibraryService">
    
                    <clear />
                    <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" />
                    <endpoint binding="basicHttpBinding" name="DocumentLibraryService" contract="Peninsula.DocumentLibrary.ServiceLayer.IDocumentLibraryService" address=""
                              bindingConfiguration="BasicHttpBinding_IDocumentLibraryService"/>
                </service>
    

    【讨论】:

    • 这次1000!!我失去了两天的头撞墙,试图找出我的配置出了什么问题。我设置了所有正确的配额,但仍然出现错误。谢谢!
    【解决方案2】:

    这不是一个真正的答案,因为您的配置看起来不错。我认为您只需要检查 服务主机代理 上的代码(输出到跟踪或调试)中的这些值,以确保加载配置中的相同值进入您的频道。

    可能是达到了另一个阈值并且错误具有误导性

    现在我强烈建议不要使用字节数组上传文件,尤其是如果您使用 XML。这些将表示为 XML 数组,结构将是非常臃肿的 XML,这将比原始文件多 10 倍。

    我会使用:

    • WCF Streaming 也适用于基本绑定,并且超级快
    • 或者将字节数组表示为 base64 字符串。这将多占用 33% 的空间,但不会增加 1000%

    更新

    您可以跟踪用于配置服务的绑定名称(在您的任何 WCF 操作中使用它):

    public int MyServiceOperation()
    {
         Trace.WriteLine(OperationContext.Current.EndpointDispatcher.ChannelDispatcher.BindingName)
    ....
    

    【讨论】:

    • 感谢您的回复 Ali Jan,我的服务托管在 IIS 上。如何在 IIS 上检查这些值?
    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 2011-03-05
    • 1970-01-01
    • 2013-09-15
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    相关资源
    最近更新 更多