Friday 23 May 2014

Value cannot be null. Parameter name: s solution

I recently worked on a project where we were getting the error message below in our log files. This mysterious issue plagued us for weeks and we did not understand why we were getting the error and even how it impacted the user. We only knew that it would come in about 1 of 300 page views.

We were using a custom PageStatePersister and saving Asp.Net Web Forms Viewstate to a database. The error never showed up in the development environment.

We tried all sorts of things to reproduce it and make it go away. After some time trying to reproduce the error we managed to reliably reproduce it locally by doing simultaneous page requests for different web pages. Any request type async/non-async, postback/non-postback would trigger it. For end users it would manifest itself as annoying glitches or hiccups with possible loss of input data.

We finally managed to also locate the error after days of changing one by one line of code back to an older version.

The error was caused by someone having redefined the LosFormatter variable as static in our PageStatePersister.

Changing the LosFormatter variable definition to non static immediately solved the problem and also improved performance.

Exception type: System.ArgumentNullException

Value cannot be null.
Parameter name: s

   at System.Web.UI.ObjectStateFormatter.DeserializeIndexedString(SerializerBinaryReader reader, Byte token)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader)
   at System.Web.UI.ObjectStateFormatter.Deserialize(Stream inputStream)