HTTP数据包中可以返回的数量是否有限制?或者这是WCF JSON限制?

时间:2012-06-07 14:48:12

标签: json wcf http serialization limit

我正在使用自托管(不在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说没有响应请求)。

3 个答案:

答案 0 :(得分: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

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

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

答案 1 :(得分:0)

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

尝试检查设置

答案 2 :(得分:0)

根据序列化的每条记录中的项目数量,您可能会遇到maxItemsInObjectGraph的{​​{1}}属性的默认限制。 This MSDN article讨论了该属性,但确实包含错误。默认值为65536而不是Int32.MaxValue。