We have a web solution with mixed C# and VB.NET (3.5) projects (note: several vb.net files do have Option Strict Off). Our code runs as modules inside DotNetNuke 4.8. Under certain conditions, our application will crash at the method:
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteString
The abbreviated message in that exception is:
System.ArgumentException: "Error serializing value XYZ of type XYZ."
System.InvalidCastException: "Unable to cast object of type 'System.Int64'
to 'System.String'"
Nowhere in the stack trace is our code, it’s just System code that ends up failing to serialize type XYZ from our code. This means I can’t hit a break point and debug which exact property of XYZ is the problem. The stack trace property is empty, but in the Message is a stack trace abbreviated as:
DotNetNuke.Services.Exceptions.PageLoadException: "Error serializing value 'XYZ' of type 'XYZ.'"
System.ArgumentException: "Error serializing value 'XYZ' of type 'XYZ.'"
System.InvalidCastException: "Unable to cast object of type 'System.Int64' to type 'System.String'. "
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteString(NameInfo memberNameInfo, NameInfo typeNameInfo, Object stringObject)
// ...... etc.........
at System.Web.UI.Page.SaveAllState()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
// --- End of inner exception stack trace
All relevant lines in this trace start with System.. The top lines are DotNetNuke. However, following the first answer’s suggestion I compiled the DotNetNuke (4.8) code myself and turns out that code just does Server.GetLastError, showing the InvalidCastException as the InnerException without any additional info on the actual value or property name being serialized.
The question then is: how do I discover which field and value the serializer is having trouble with?
A few related questions/problems that came to mind while researching this problem:
- If I look at the Formatters.Binary part of MSDN I’d guess the ObjectWriter is internal for Binary (as suggested in the comments, red)?
- How can .NET even at all fail at casting a Int64 to String? Rephrased: what value for a Int64 could potentially not be converted to a String? However, as pointed out in comments: Int64 can easily be converted to Strings, but Casting them is a different issue.
- The Serialize method doesn’t announce this ArgumentException or InvalidCastException may occur (in the Exceptions section).
I tried investigating XYZ and types for properties in that class in ildasm to see any irregularities but couldn’t find any.
Answering my own question, hopefully that will help anyone ending up here after a search query.
The final answer was found in this SO question. I ended up using a trial version of the .NET Reflector (and the included Visual Studio 2010 plugin) to step into the mscorlib code. This allowed me to inspect the NameInfo for the property that was causing the InvalidCastException.
In short, the answer was: use .NET Reflector (or a similar tool) to step into mscorlib.
PS. While stepping into mscorlib was the answer to my problem posed in this question, there’s still an underlying problem which probably warrants a whole new SO question, once I grab enough info on it.