How to debug deserialization errors in .NET?
.NET's Deserilizatio开发者_开发知识库n errors are quite generic, for example something like this:
System.ArgumentException: Object of type 'System.Uri' cannot be converted to type 'System.String'.
It's clear that we changed the type of a property in an object but there are like 10-15 different classes in this serialized object, so it's really hard to figure out which one we changed or which commit messed this up.
Is there anyway to get information about which property in which class (or at least in which class) actually causing this error? Is there any external tool or known ways to do this?
P.S. Before anyone start telling me why I shouldn't use binary serializer or why I should X,Y instead etc. for backward compatibility, please save the advice on those. I'm aware of all those but that's not the question.
If you enable debugging into framework code (see this link) and then press ctrl + shift + e and select all managed code exceptions the error will appear in the actual source line that fails. You should be able to use the stack trace then to find out what part of the object it was trying to deserialize at that point.
It's not easy, but that's how we ended up doing it.
There's a couple of different things you can do, none of them great. Especially with binary serialization. You could add custom serialization handling with the ISerializable interface, which would allow you to step through the deserialization process in the debugger.
Have you considered switching to Xml serialization for development/debugging purposes? There's a few more hooks that you can use with Xml serialization. But it sounds like that won't work for you, as you're probably dealing with either a remote interface or older binary data stored on disk that you need to read.
But even easier would be to look through your source control system's logs for the method with the changed type.
Looking at the Inner Stack trace can be useful. The serializer generates a class specifically to handle the type you are wanting to serialize/deserialize.
Just by looking at the function names of the functions involved in the stacktrace you can track down which node node is breaking.
This helps narrowing down where the problem may lie, especially with large complex XML files.
Example:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Xml.XmlConvert.ToInt32(String s)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXML.Read13_Item(Boolean isNullable, Boolean checkType) //Tells you the issue is on the RootNodeSubNodeSubSubNode on an item withing it.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXML.Read17_RootNodeSubNodeSubSubNode(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXML.Read26_RootNodeSubNode(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXML.Read50_RootNode(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXML.Read51_RootNode()
精彩评论