3

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

错误:

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

服务器配置:

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

3 回答 3

3

readerQuotas是一组额外的约束,WCF 实施以提供对拒绝服务 (DOS) 攻击的保护。

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

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

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

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

于 2012-06-25T12:50:44.067 回答
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>
于 2012-06-25T13:26:14.867 回答
1

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

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

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

于 2012-06-20T20:50:43.083 回答