How to identify a field that causes binary serialization to fail in .NET?

I am trying to serialize an object graph in .NET using the following method:

public static byte[] Serialize(object data) { var binary = new BinaryFormatter(); using (var ms = new MemoryStream()) { binary.Serialize(ms, data); return ms.ToArray(); } } 

However, I encountered the following error:

 FormatException: Input string was not in a correct format. Stack Trace: 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.String.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value, IFormatProvider provider) at System.Runtime.Serialization.Formatters.Binary.__BinaryWriter.WriteValue(InternalPrimitiveTypeE code, Object value) at System.Runtime.Serialization.Formatters.Binary.__BinaryWriter.WriteMember(NameInfo memberNameInfo, NameInfo typeNameInfo, Object value) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo, Object data) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteMembers(NameInfo memberNameInfo, NameInfo memberTypeNameInfo, Object memberData, WriteObjectInfo objectInfo, NameInfo typeNameInfo, WriteObjectInfo memberObjectInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteMemberSetup(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo, String memberName, Type memberType, Object memberData, WriteObjectInfo memberObjectInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo, String[] memberNames, Type[] memberTypes, Object[] memberData, WriteObjectInfo[] memberObjectInfos) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) 

Is there an easy way to determine which field is generating this error? I could recursively mark the fields in the object graph as NonSerialized to narrow down the potential culprits, but since the object graph is quite extensive, it is cumbersome and seems unnecessary.

Note that I'm not sure why BinaryFormatter cannot serialize one or more values ​​in an object graph. If an object can be stored in memory at run time, it is not clear why it cannot be serialized. Could this be a listing problem?

+7
source share
3 answers

Use Windbg. Download here (select only the debugger from the installer. You do not need to fully pack the SDK) and run it.

Then use File - Open Executable - and run it. You will be broken into exception in the debugger. If you have not chosen, before you begin

 Debug - Event Filters - CLR Exception - Enabled 

to enable a breakpoint for each managed exception. Then you need to enter

 .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 that were used by the current thread before it works. Then you click on one of the blue underlined NameInfo instances to find out in which member variable the serializer failed. I agree that it takes some patience to learn, but you can debug such things in record time when others need to mess around in their code to solve the problem. All you have to do is look into the NameInfo instance that caused the problem.

+6
source

What I was referring to was to serialize the object to a string and then write the string to a file. Then you can look at the serialized line, see where it stopped, and make a conclusion about which element this caused the problem.

+3
source

Comment on all properties and serialize the object. Re-enter them one at a time until the error returns.

This is basic debugging.

The stack trace gives hints, although some types are not serialized.

-one
source

All Articles