How can i determine which object in an object graph caused a SerializationException
I'm deserializing an object and in certain cases it works fine, however in others it fails. The exception is essentially meaningless to me, but there has to be a way to figure out where exactly it failed so i redirect my debugging.
This is the exception:
System.Runtime.Serialization.SerializationException was caught Message="No map开发者_StackOverflow社区 for object '201326592'."
Source="mscorlib" StackTrace: at System.Runtime.Serialization.Formatters.Binary._BinaryParser.ReadObject() at System.Runtime.Serialization.Formatters.Binary._BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) at Analytics.ReportInstance.Open(Stream tStream, Boolean OpenResults) in C:\Users...path...\File.vb:line 149 InnerException:
And this is the source code where it is trapped:
Public Shared Function Open(ByVal tStream As IO.Stream, Optional ByVal OpenResults As Boolean = False) As ReportInstance
Dim tFormatter As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = PluginSerializationBinder.CreateSerializer()
Dim tInstance As ReportInstance
Try
If OpenResults Then 'case which always fails
'open the entire report
If System.Diagnostics.Debugger.IsAttached Then
Console.WriteLine("Deserializing: report results")
End If
tInstance = tFormatter.Deserialize(tStream) 'throws exception here
Return tInstance
Else 'case which always works (only deserializing part of the object)
'just open the stub
If System.Diagnostics.Debugger.IsAttached Then
Console.WriteLine("Deserializing: report instance")
End If
Dim tInput As New IO.BinaryReader(tStream)
Dim tLength As Long = tInput.ReadInt64()
Dim tBytes() As Byte = tInput.ReadBytes(tLength)
Dim tMem As New IO.MemoryStream(tBytes)
tInstance = tFormatter.Deserialize(tMem)
Return tInstance
End If
Catch ex As Exception
If (ex.Message.Contains("blah")) Then
Throw New UnsupportedException(ex.Message)
Else
Throw 'trapped here
End If
End Try
End Function
Thanks, brian
The exception you're seeing is thrown when the object's "map ID" -- an integer identifying its type, which should reference a type definition earlier in the stream -- is not found in the table of types.
Normally this should not happen unless the byte stream is mangled in transit -- or a formatter instance is reused inappropriately.
A BinaryFormatter
keeps track of everything it has already processed, and can emit back-references to previously written types and objects (when serializing) or use previously-read data to resolve back-references in current data (when deserializing).
精彩评论