How do you identify the field that is causing binary serialization to fail in .NET?

大城市里の小女人 提交于 2019-12-04 04:01:27

Use Windbg. Download it here (select from the installer only the debugger. You do not need to downlad the complete SDK) and start it.

Then use File - Open Executable - and start it. You will break at the exception in the debugger. If not select before you start

Debug - Event Filters - CLR Exception - Enabled

to enable a breakpoint on every managed exception. Then you need to type

.loadby sos clr 
(if you are using .NET 3.5 .loadby sos mscorwks)
.prefer_dml 1
!dso

This will give you a list with the objects which were used by the current thread before it did fail. Then you click on one of the blue underlined NameInfo instances to see which at which member variable the serializer did fail. I agree that it requires some patience to learn but you can debug such stuff in record time where others need to fiddle around in their code to nail the issue. All you need to do is to look into the NameInfo instance which did cause the issue.

The way that I approached this was to serialize the object to a string and then write the string to a file. You can then look at the serialized string, see where it stopped, and infer from there which element it was that caused the problem.

Comment out all the properties, and serialize the object. Reintroduce them one at a time until the error returns.

This is basic debugging.

The stack trace gives hints though, if there aren't to many types being serialized.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!