Конец потока, обнаруженный до завершения анализа при десериализации

Я получаю выше ошибку при десериализации BinaryFile для объекта. Я попытался установить положение =0, метод seek.begin и попытался с потоком памяти, но бесполезно. Каждый раз, когда я получаю ту же проблему. Кто-нибудь может мне помочь? Ниже мой фрагмент кода для сериализации и десериализации

Сериализация

Dim fs As New FileStream(Filename, FileMode.OpenOrCreate)
Try

    'fs = New FileStream(Filename, FileMode.OpenOrCreate)
    Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    bf.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
    bf.TypeFormat = Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded
    bf.Serialize(fs, data)
Catch ex As Exception
    MsgBox("Exception: " & ex.Message & " | Stacktrace: " & ex.StackTrace)
Finally
    fs.Flush()
    fs.Close()
    fs.Dispose()

End Try

Десериализация

Dim fs As FileStream = Nothing

Try
    fs = IO.File.OpenRead(Filename)
    'fs = New FileStream(Filename, FileMode.Open)

    Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()

    bf.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
    bf.TypeFormat = Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded
    fs.Seek(0, SeekOrigin.Begin) 'fs.Position=0
    Dim obj As Object = bf.Deserialize(fs)
    Return obj
Catch ex As Exception
    MsgBox("There was an exception while trying to convert binary file to object. Exception: " & ex.Message & " | Stacktrace: " & ex.StackTrace)
Finally
    If fs IsNot Nothing Then
        fs.Flush()
        fs.Close()
        fs.Dispose()
    End If
End Try

0 ответов

Другие вопросы по тегам