Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug deserialization errors in .NET?

Tags:

.NET's Deserilization 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.

like image 771
dr. evil Avatar asked Feb 03 '11 13:02

dr. evil


People also ask

How do you debug a serializer?

You can access the window from Tools > Odin Inspector > Serialization Debugger and select your script type from the dropdown to start debugging. Or you can directly start debugging a component by clicking the Debug Serialization button in the component's cogwheel dropdown menu.

How does Deserialization work in C#?

Deserialization is the process of reconstructing an object from a previously serialized sequence of bytes. It allows us to recover the object whenever it is required. It is the reverse process of serialization. Deserialize() method of BinaryFormatter class is used for deserialization from binary stream.

What is Deserialize error?

Hence when the XML stream comes in, and the proxy tried to de-serialize (parse) to create ABAP object, it fails. This is called DESERIALIZATION ERROR. You mentioned that you could successfully get a single record, while when multiple records came from server, it failed.

What is the meaning of Deserialization?

Deserialization is the opposing process which takes data from a file, stream or network and rebuilds it into an object. Serialized objects can be structured in text such as JSON, XML or YAML. Serialization and deserialization are safe, common processes in web applications.


2 Answers

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.

like image 129
Matthew Steeples Avatar answered Oct 12 '22 23:10

Matthew Steeples


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.

like image 37
MonkeyWrench Avatar answered Oct 13 '22 00:10

MonkeyWrench