【问题标题】:Is there a limit on the amount that can be returned in an HTTP packet? Or is this WCF JSON limit?HTTP 数据包中可以返回的数量是否有限制?或者这是 WCF JSON 限制?
【发布时间】:2012-06-07 14:48:11
【问题描述】:

我正在使用自托管(不在 IIS 中)WCF 数据服务,它通过 GET 接收 REST 调用并返回 JSON。

我可以返回 3800 条记录,但是当我转到 3900 时它失败了。如果我没有收到来自 WCF 或 .NET 的错误或警告事件,应用程序将继续为新请求完美运行。它只是默默地丢弃结果,不会将数据序列化为 JSON:

这是 3800 条记录的返回值:

HTTP/1.1 200 OK
Content-Length: 1958039
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Access-Control-Allow-Origin: *
Date: Thu, 07 Jun 2012 14:28:39 GMT

{"count":3800,"results":[{"bbox":"18.57544760000000,- .......

这是服务合同:

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    CatalogResults SearchBoxADO(string requestBox);

在调试器中,SearchBoxADO 返回 3900 条记录没有问题,但它没有被序列化,也没有生成 HTTP 响应(Fiddler 说对请求没有响应)。

【问题讨论】:

  • 我经常在get中发送多个mo的json文件。

标签: json wcf http serialization limit


【解决方案1】:

它是 WCF 框架的默认设置。为了从 WCF REST 服务中获取更大的数据集,您需要在客户端和服务器端增加 readerQuotas,如下所示:

<binding maxBufferPoolSize="655360" maxReceivedMessageSize="655360">
         <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647"
              maxNameTableCharCount="2147483647" />
        <security mode="None" />
      </binding>

还可以考虑将 maxItemsInObjectGraph 设置为一个较大的值,如下所示:

<behavior>
    <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
</behavior>

您可以通过如下代码实现上述功能:

由于您通过代码配置所有内容,您甚至可以使用代码定义 readerQuotas,如下所示:

Binding binding = new WebHttpBinding();
binding.MaxBufferSize = 2147483647;
binding.MaxReceivedMessageSize = 2147483647;

var myReaderQuotas = new XmlDictionaryReaderQuotas();
myReaderQuotas.MaxStringContentLength = 2147483647;
myReaderQuotas.MaxDepth = 2147483647;
myReaderQuotas.MaxArrayLength = 2147483647;
myReaderQuotas.MaxBytesPerRead = 2147483647;
myReaderQuotas.MaxNameTableCharCount = 2147483647;
binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); 

如果您尝试在浏览器中获取数据,那么我想如果您使用任何 .NET 应用程序作为客户端,那应该可以工作,那么您需要定义与为服务器指定的相同的 readerQuotas

您可以通过代码设置 maxItemsInObjectGraph,方法是将 ServiceBehavior 属性添加到您的类中,如下所示:

[ServiceContract]
[ServiceBehavior(MaxItemsInObjectGraph = 2147483647)]
public class Test
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    CatalogResults SearchBoxADO(string requestBox);
}

【讨论】:

    【解决方案2】:

    这可能是由一些与内容大小相关的绑定设置引起的,例如:来自 ReaderQuotas 的 maxBufferSize、maxBufferPoolSize 或 maxStringContentLength 属性。

    尝试检查设置

    【讨论】:

      【解决方案3】:

      根据要序列化的每条记录中有多少项目,您可能会遇到dataContractSerializermaxItemsInObjectGraph 属性的默认限制。 This MSDN article 讨论了该属性,但它确实包含错误。默认值为 65536 而不是 Int32.MaxValue。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-14
        相关资源
        最近更新 更多