我使用的是一个自托管(不在i is中)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
{“计数”:3800,“结果”:[{“bbox”:“18.57544760000000,-…….
这是服务合同:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
CatalogResults SearchBoxADO(string requestBox);
在调试器中,searchboxado返回的3900条记录没有问题,但它没有被序列化,也没有生成http响应(fiddler说没有响应请求)。
最佳答案
它是wcf框架的默认设置。为了从wcf rest服务获得更大的数据集,您需要在客户端和服务器端增加readerquota,如下所示:
<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>
您可以通过如下代码实现上述功能:
由于您是通过代码配置所有内容的,因此您甚至可以使用如下所示的代码定义readerquota:
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
通过向类中添加servicebhavior属性,可以通过代码设置maxitemsinobjectgraph,如下所示:
[ServiceContract]
[ServiceBehavior(MaxItemsInObjectGraph = 2147483647)]
public class Test
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
CatalogResults SearchBoxADO(string requestBox);
}