Wednesday, December 5, 2012

What does Operation is not valid due to current state of the object exception mean?


Error message

System.InvalidOperationException: Operation is not valid due to the current state of the object.

Stack trace

[InvalidOperationException: Operation is not valid due to the current state of the object.]
at System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded()
at System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding\ encoding)
at System.Web.HttpRequest.FillInFormCollection()

[HttpException: The URL-encoded form data is not valid.]
at System.Web.HttpRequest.FillInFormCollection()
at System.Web.HttpRequest.get_Form()
at System.Web.HttpRequest.get_HasForm()
at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)
at System.Web.UI.Page.DeterminePostBackMode()
at System.Web.UI.Adapters.PageAdapter.DeterminePostBackMode()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)

Cause:
  • Microsoft’s Security update MS11-100 released on December 28, 2011, limits the max no. of form keys, files and JSON members to 1000 in an HTTP request. 
  • ASP.NET rejects requests that have more than 1000 of these elements and the user sees an error in the browser.
  • In the older version of ASP.NET( i.e prior to this update) HTTPValueCollectionClass did not have the ThrowIfMaxHttpCollectionKeysExceeded method. This method was introduced in the class after this update. So if the request contains more than 1000(default value) elements, we see this exception.
Solution: 
Increase the value of key aspnet:MaxHttpCollectionKeys to a higher number for example 1500.


Side-effect
The amount we specify has an impact on upload speed of our content, which will be proportionally slower. Therefore, it’s better to set the value, that should be enough for the exception to not occur.

No comments:

Post a Comment