简体   繁体   English

如何确定对象图中的哪个对象引起了SerializationException

[英]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 for object '201326592'." “ System.Runtime.Serialization.SerializationException被捕获Message =”对象'201326592'的未映射。”
Source="mscorlib" StackTrace: at System.Runtime.Serialization.Formatters.Binary._ BinaryParser.ReadObject() at System.Runtime.Serialization.Formatters.Binary. Source =“ mscorlib” StackTrace:位于System.Runtime.Serialization.Formatters.Binary._ BinaryParser.ReadObject(),位于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: System.Runtime.Serialization.Formatters.BinaryBinary.in(System.Runtime.Serialization.Formatters。 C:\\上C:\\用户...路径... \\ File.vb:第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. 当在类型表中找不到对象的“映射ID”(标识其类型的整数,应在流中更早引用类型定义)时,将引发您所看到的异常。

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). BinaryFormatter跟踪其已处理的所有内容,并可以发出对先前写入的类型和对象的反向引用(在序列化时),或使用先前读取的数据来解析当前数据中的反向引用(在反序列化时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM