Working with JSON within Web Services recently has become the latest and greatest simply because it plays so nicely with others and can often be very easily serialized and deserialized to fit your needs.
There is just something rewarding about having a massive collection of complex objects that can be thrown into a simple string and passed across however you see fit and then completely regenerated in its original collection in just a handful of code.
However sometimes these strings can get big and I mean really big, like exceeding default values big, and when this happens, exceptions happen. This post will cover a few ways that you can prevent a nasty InvalidOperationException that may look something like this with ease:
The Problem
You decide that paging data is for losers and decide to pull a graph of all of the data within your database, which will hypothetically contain thousands of complex objects, each with additional properties of their own. If your dataset is large enough, you’ll find yourself quickly exceeding the bounds that your Serializer can handle and you’ll be met with an exception and no data.
The Solution
There are three basic methods of handling this depending on your current environment:

- Setting the MaxJsonLength property default value within your web.config. (This will only apply to web-services that handle JSON).
- Setting the MaxJsonLength property of a JavascriptSerializer object to perform your serialization.
- If you are using MVC4 to handle returning your JSON values, you may want to override the default JsonResult() ActionResult and change the maximum size manually.Either of these changes will help you avoid any bumps in the road when passing across your large JSON values.
Option I: The MaxJsonLength Property for handling JSON in Web Services
- <configuration>
- <system.web.extensions>
- <scripting>
- <webServices>
- <!-- Update this value to change the value to
- a larger value that can accommodate your JSON
- strings -->
- <jsonSerialization maxJsonLength="86753090" />
- </webServices>
- </scripting>
- </system.web.extensions>
- </configuration>
Option II: Using the JavaScriptSerialzier and the MaxJsonLength Property to Handle Serializing JSON Values
- // Creates an instance of your JavaScriptSerializer
- // and Setting the MaxJsonLength
- var serializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };
- // Perform your serialization
- serializer.Serialize("Your JSON Contents");
Option III: Overriding the MVC4 JsonResult Action Result to Handle the Maximum Size
- return new JsonResult() {
- Data = "Your Data",
- MaxJsonLength = 86753090
- };
- JsonResult result = Json("Your Data");
- result.MaxJsonLength = 8675309;
- // This ActionResult will override the existing JsonResult
- // and will automatically set the maximum JSON length
- protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
- {
- return new JsonResult()
- {
- Data = data,
- ContentType = contentType,
- ContentEncoding = contentEncoding,
- JsonRequestBehavior = behavior,
- MaxJsonLength = Int32.MaxValue // Use this value to set your maximum size for all of your Requests
- };
- }

Santhakumar MunuswamyPosted Nov 21, 2015, 9:47 AM
Good article.
Christopher AndrewsPosted Nov 18, 2015, 5:58 AM
The article was extremely helpful, thank you. I found this .net error tracking tool amazingly simple and nice www.redfly.io
Rupali ShindePosted Nov 17, 2015, 1:11 AM
good job sir
Debendra DashPosted Oct 31, 2015, 8:38 AM
nice.....
Sibeesh VenuPosted Oct 31, 2015, 6:13 AM
Nice Share
Nilesh JadavPosted Oct 31, 2015, 3:49 AM
Good Share !
Gowtham KPosted Oct 31, 2015, 3:17 AM
Good One, Thanks for sharing
Harshad PansuriyaPosted Oct 31, 2015, 12:04 AM
Nice one
Ravi PatelPosted Oct 30, 2015, 11:40 PM
good one thanks sir
Shridhar SharmaPosted Oct 30, 2015, 3:18 PM
thanks for sharing sir
Upendra Pratap ShahiPosted Oct 30, 2015, 1:56 PM
nice info...thanks..
Priyaranjan K SPosted Oct 30, 2015, 1:36 PM
Thanks for the share