Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wcf

wcf-client

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();
                    }
                }
            }
like image 484
Troy Barlow Avatar asked Nov 25 '25 17:11

Troy Barlow


1 Answers

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.

like image 106
Dmitry Harnitski Avatar answered Nov 27 '25 12:11

Dmitry Harnitski