Search code examples
.netjsonweb-serviceswcffiddler

JSON Increase Maximum Number of Objects


I have a WCF web service built in C#.NET that is returning many many records (13k+) from a SQL database, being returned in JSON format. I'm running into a problem with the number of records being returned in the web service call- the problem appears to be the sheer number of objects, not the amount of data.

I found this answer on SO that claims the max number of "objects" allowed in a JSON return is limited to around 65k. After returning my 13k+ records with all of their sub-properties (6 each) I appear to have surpassed that limit. If I try to make the call to my service and return all records, I get the following error headers in Fiddler:

HTTP/1.1 504 Fiddler - Receive Failure
Date: Tue, 29 Jul 2014 13:16:18 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate

However, if I switch my Stored Procedure in SQL to only return TOP 10000, everything works fine and my data comes through perfectly.

As stated earlier, it doesn't appear to be the size of the returned object- by scaling up from the amount of data that TOP 10000 returns, my full result set would be around 1.4-1.5 MB, and I have my web.config file set up to allow a max JSON size of much much more than that:

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="wellOver10GB" />
    ...

Has anyone else run into this problem of too many objects and found a solution? Is there a way to increase this limit, or is my problem possibly unrelated to this limit and actually something else I'm not seeing?


Solution

  • There is a property ServiceBehaviorAttribute.MaxItemsInObjectGraph that needs to be adjusted in order to return more objects. This can be done in the following way in the WCF project:

    ...
    namespace _WSProjectName
    {
        [ServiceBehavior(MaxItemsInObjectGraph = 1000000)] // Just did a million in my case, can be whatever you want to set it to
        public class ServiceName : IServiceName
        {
            ...
    

    According to the MSDN documentation on ServiceBehaviorAttribute.MaxItemsInObjectGraph:

    Use the MaxItemsInObjectGraph property set a limit on the number of items in an object graph that are serialized.

    The documentation states that the default value is 65,536 (2^16)