HttpClient将byte []发布到WCF服务会产生错误:最大数组长度配额(16384)或最大项目

时间:2012-06-19 13:44:00

标签: wcf wcf-client

我有一个可以接受byte[]的WCF服务。我正在使用HttpClient创建客户端并收到以下错误。我在线阅读您必须在服务器和客户端上设置readerQuotas,但如何在HttpClient上设置这些设置?

错误:

  

反序列化RTM.API.Resources.UGCRequest类型的对象时出错。读取XML数据时,已超出最大数组长度配额(16384)或对象图配额中的最大项目。可以通过更改XmlDictionaryReaderQuotas或MaxItemsInObjectGraph设置上的MaxArrayLength属性来增加这些配额。

服务器配置:

    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"/>
            <standardEndpoint name="DirectoryEndpoint"/>
        </webHttpEndpoint>
    </standardEndpoints>
    <services>
        <service name="API.Service.UGCService" behaviorConfiguration="DataServiceBehavior">
            <endpoint contract="API.Service.UGCService" kind="webHttpEndpoint" endpointConfiguration="" binding="webHttpBinding" bindingConfiguration="BigHttpBinding"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="DataServiceBehavior">
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <dataContractSerializer maxItemsInObjectGraph="2147483644"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <webHttpBinding>
            <binding name="BigHttpBinding" transferMode="Buffered" maxReceivedMessageSize="2147483647" >
                <readerQuotas maxArrayLength="2147483647" maxDepth="2147483647" maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
            </binding>
        </webHttpBinding>
    </bindings>

客户端代码:

using (HttpClient client = new HttpClient(apiPath))
            {
                using (HttpRequestMessage request = new HttpRequestMessage(method, finalUrl))
                {
                    request.Headers.Accept.AddString("application/json");
                    request.Headers.Add("Authorization", sb.ToString());

                    if (method == "POST" || method == "PUT")
                    {
                        if (requestBody.Count() == 0)
                            request.Headers.ContentLength = 0;
                        else
                        {
                            request.Content = HttpContent.Create(APM6.Utils.Serialize(requestBody), "application/json");
                        }
                    }

                    using (HttpResponseMessage response = client.Send(request))
                    {
                        return response.Content.ReadAsString();
                    }
                }
            }

3 个答案:

答案 0 :(得分:3)

readerQuotas是WCF实施的一组附加约束,用于防止拒绝服务(DOS)攻击。

HttpClient没有实现该保护,因此您无需担心额外的客户端配置。

您会看到该错误,因为<readerQuotas maxArrayLength="2147483647" />由于某种原因未应用于您的服务器端点。您可以使用svcutil.exewcftestclient.exe来证明这一点。

尝试使用这些工具生成配置文件,您可能会在该配置文件中看到<readerQuotas maxArrayLength="16384" />。这意味着服务器部分未将扩展值应用于maxArrayLength

如果是这样,请使用服务器端配置或代码以确保应用配置。

答案 1 :(得分:2)

试试这个:

  <endpointBehaviors>
    <behavior name ="namespace1.namespace1Behavior">
      <dataContractSerializer maxItemsInObjectGraph ="2147483647"/>
    </behavior>
    <behavior name="mybehavior">
      <transactedBatching maxBatchSize="2147483647"/>
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="namespace1.namespace1Behavior">
      <dataContractSerializer maxItemsInObjectGraph ="2147483647"/>
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true.  Set to false before deployment 
      to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="True" />
    </behavior>
  </serviceBehaviors>
</behaviors>

答案 2 :(得分:1)

在这种情况下,客户端上唯一需要的设置是MaxResponseContentBufferSize

但是,在服务器上反序列化期间出现了问题。您可能需要添加maxBufferPoolSize="2147483647" maxBufferSize="2147483647"作为绑定参数

这仍然不完全对应于错误消息。确保服务行为对应于问题中显示的配置文件,而不是某个不同的副本。