HttpClient 将字节 [] 发布到 WCF 服务产生错误:最大数组长度配额 (16384) 或最大项目 [英] HttpClient posting byte[] to WCF service produces error: The maximum array length quota (16384) or the maximum items

查看:24
本文介绍了HttpClient 将字节 [] 发布到 WCF 服务产生错误:最大数组长度配额 (16384) 或最大项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

服务器配置:

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

推荐答案

readerQuotas 是一组额外的约束,WCF 实施这些约束以防止拒绝服务 (DOS) 攻击.

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

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

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

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

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.

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

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.

这篇关于HttpClient 将字节 [] 发布到 WCF 服务产生错误:最大数组长度配额 (16384) 或最大项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆