How to overcome MaxItemsInObjectGraph limit when using a simple HTTP GET on a RESTful WCF service?
-
2012年3月7日 下午 09:12
From the service trace, I got this when I try to query a large mount of data (smaller set of data works):
"Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota."
I am well aware of the suggestions on this forum to put custom values on both service and client side like this:
<dataContractSerializer maxItemsInObjectGraph="80000000"/>
My question is, if I implement a simple HTTP GET using HttpWebRequest and HttpWebResponse, how can I configure the MaxItemsInObjectGraph value? Better yet, supposedly this should work with a browser too (which shouldn't required the configuration at all):
http://myservericeendpoint/getallusers
Here is my service configuration:
<system.serviceModel>
<services>
<service behaviorConfiguration="webRequest" name="VCWebServer.VCWebServiceType">
<endpoint address="http://localhost/VCWebService" behaviorConfiguration="webby"
binding="webHttpBinding" contract="VCWebServer.IVCWebService" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="secure" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="webRequest">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>Service Code:
namespace VCWebService
{
[ServiceContract]
public interface IVCWebService
{[OperationContract]
[WebGet(UriTemplate = "/GetAllUsers")]
ADUsers GetAllADUsers();
}
[CollectionDataContract(Name = "adusers", Namespace = "")]
public class ADUsers : List<ADUser>
{
}[DataContract(Namespace = "", Name = "aduser")]
public class ADUser
{
[DataMember(Name = "Id")]
public string Id;[DataMember(Name = "givenName")]
public string givenName;
[DataMember(Name = "sn")]
public string sn;
[DataMember(Name = "initials")]
public string initials;
}}
string url = "http://serveraddress/VCWebService/GetAllUsers";
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();Thanks!
- 已編輯 robyNHelp 2012年3月8日 下午 03:31
所有回覆
-
2012年3月8日 上午 09:40Does it not work even after setting the readerQuotas and maxItemsInObjectGraph on the server. I have had the same issue but once i set them on server it works without any problem for large data when i invoke using HttpWebRequest
Rajesh S V
-
2012年3月8日 下午 12:02版主Hello, as Rajesh pointed out, you simply need to set readerQuotas to a larger value on the service side. If you wonder how to configure the same on the client, well, at least for HttpWebRequest and JavaScript's XMLHttpRequest, you don't need to do anything. Unlike WCF, those platforms do not have a throttling mechanism. So there's nothing to configure on the client side.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email. -
2012年3月8日 下午 03:40
Hi Rajesh,
As you can see from the highlights, I did set readerQuotas and maxItemsInObjectGraph on the server side. Unless I set it wrong somehow. How do you host the RESTFul WCF service? I just use a console application to self host for testing:
string baseUri = "http://localhost/VCWebService";
WebServiceHost sh = new WebServiceHost(typeof(VCWebServiceServiceType), new Uri(baseUri));
sh.Open();I don't think it matters?
Thanks.
-
2012年3月8日 下午 03:43
Hi Yi-Lun,
That is exactly what I thought. As you can see I already set the values bigger on the server side. I am wondering maybe somehow I set it wrong or because the way I host the service?
Thanks.
-
2012年3月8日 下午 04:09
I generally host my wcf services on IIS. But self hosting should not give any problem.
What is the error you get after setting it to large values and when you try to invoke it from your client?
Check the trace log as well
Rajesh S V
-
2012年3月8日 下午 04:20
Hi Rajesh,
The error from the client side is:
The remote server returned an error: (504) Gateway Timeout.
This is the service trace log:
"Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota."
Thanks.
-
2012年3月8日 下午 04:32
can you just hit the same service from IE and see if that comes back without any problem rather than from your code.
Make sure that you have set the HttpRuntime maxRequestLength property in case your data is exceeding more than 4MB
Rajesh S V
-
2012年3月9日 上午 01:43版主
Looks like you give the binding a name:
<binding name="secure"...
But you haven't actually used it in your endpoint. You need to add a bindingConfiguration to your endpoint:
<endpoint address="http://localhost/VCWebService" bindingConfiguration="secure"...
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email. -
2012年3月9日 下午 04:03
Hi Rajesh,
I will try to do that a little later, although eventually I cannot use IIS to host it since some of our clients just don't want IIS running at all due to perceived security risks.
-
2012年3月9日 下午 04:05
Hi Yi-Lun,
I tried it and still the same errors, but thanks for pointing that out, because it may prevent some other potential issues in the future.
-
2012年3月14日 下午 05:14
Hi Guys,
I have a complete project and sample db for you to test. Any taker to solve this mystery?
Thanks.
-
2012年3月15日 上午 09:03
Hi,
Can you email the sample project to rajeshsv.5@gmail.com
Rajesh S V
-
2012年3月16日 下午 05:50
Hi,
I have mailed you a sample back with a new project called WCFSelfHost that resolves your problem.
If you just try to find the differences in your SelfHost compared to WCFSelfHost project you should know what were missing.
I have tested it with Fiddler and could get back a response of the complete users list of 10000 if i am not wrong.
Rajesh S V
-
2012年3月16日 下午 09:45
Thanks to Rajesh, it works now.
The small changes to the configuration and self hosting call make the difference.
Service configuration:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="VCWebService.VCWebServiceServiceType" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost/VCWebService"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="VCWebService.IVCWebService"/>
</service>
</services><behaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors></system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>Self Hosting call:
using (var host = new ServiceHost(typeof(VCWebService.VCWebServiceServiceType))) { host.Open(); Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); }- 已標示為解答 robyNHelp 2012年3月16日 下午 09:45

