如何确定对象图中的哪个对象导致SerializationException

时间:2011-01-12 22:07:37

标签: .net vb.net serialization

我正在反序列化一个对象,在某些情况下它工作正常,但在其他情况下它失败了。这个例外对我来说基本上没有意义,但必须有办法弄清楚它究竟在哪里失败,所以我重定向我的调试。

这是一个例外:

  

System.Runtime.Serialization.SerializationException   被抓了Message =“没有地图   对象'201326592'。“   Source =“mscorlib”StackTrace:          在System.Runtime.Serialization.Formatters.Binary ._ BinaryParser.ReadObject()          在System.Runtime.Serialization.Formatters.Binary。 _BinaryParser.Run()          在System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler)   处理程序,__BinaryParser serParser,   布尔值fCheck,布尔值   isCrossAppDomain,IMethodCallMessage   methodCallMessage)          在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream   serializationStream,HeaderHandler   handler,Boolean fCheck,Boolean   isCrossAppDomain,IMethodCallMessage   methodCallMessage)          在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream   serializationStream)          在Analytics.ReportInstance.Open(Stream   tStream,Boolean OpenResults)in   C:\用户... ...路径\ File.vb:行   149 InnerException:

这是被困的源代码:

    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

谢谢, 布赖恩

1 个答案:

答案 0 :(得分:1)

当对象的“map ID” - 一个标识其类型的整数(应该引用流中较早的类型定义)时,会在类型表中找不到您看到的异常。

通常情况下,除非字节流在传输过程中被破坏,否则不会发生这种情况 - 或者格式化程序实例被不适当地重用。

BinaryFormatter跟踪已经处理的所有内容,并且可以发出对先前写入的类型和对象的反向引用(在序列化时)或使用先前读取的数据来解析当前数据中的反向引用(当反串行化)。