HttpClient posting byte[] to WCF service produces error: The maximum array length quota (16384) or the maximum items

蓝咒 提交于 2019-12-12 12:26:51

问题


I have a WCF service that can accept a byte[]. I'm creating a client using HttpClient and am receiving the following error. I've read online that you have to set the readerQuotas on both the server and the client, but how do I set these settings on the HttpClient?

Error:

There was an error deserializing the object of type RTM.API.Resources.UGCRequest. The maximum array length quota (16384) or the maximum items in object graph quota has been exceeded while reading XML data. These quotas may be increased by changing the MaxArrayLength property on XmlDictionaryReaderQuotas or the MaxItemsInObjectGraph setting.

Server Config:

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

Client code:

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();
                    }
                }
            }

回答1:


readerQuotas is a set of additional constrains that WCF implements to provide protection from denial of service (DOS) attacks.

HttpClient does not implement that protection so you do not need to worry about extra client configuration.

You see that error because <readerQuotas maxArrayLength="2147483647" /> for some reason is not applied to your server endpoint. You may use svcutil.exe or wcftestclient.exe to prove that.

Try to generate config file using these tools and you will probably see <readerQuotas maxArrayLength="16384" /> in that config file. That means that server part did not apply extended value to maxArrayLength.

If that is true, play with server side config or code to make sure that configuration is applied.




回答2:


Try this:

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



回答3:


In this case the only setting that you need on the client is MaxResponseContentBufferSize.

However, your problem has occurred during deserialization on the server. You might need to add maxBufferPoolSize="2147483647" maxBufferSize="2147483647" as the binding parameters

This still does not seem to perfectly correspond to the error message. Make very sure that the service behavior corresponds to the configuration file shown in the question and not to some different copy.



来源:https://stackoverflow.com/questions/11102297/httpclient-posting-byte-to-wcf-service-produces-error-the-maximum-array-lengt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!