Serialization and BinaryFormatter version incompatibility exceptions

D

Doug Lind

Hi all,

I have seen a number of posts re: the BinaryFormatter version
incompatibility but nothing on how to recover from it. In my case, I want
the exception to trigger an alternate behaviour and continue process the
stream.

My app reads and writes configuration settings to a binary file upon
starting and shutdown. The settings are stored in various objects that are
serialized and deserialized using a BinaryFormatter. When reading the data,
my hope was to use the exception generated when the source data did not
match the target class, to force loading defaults into the target. The goal
being to trap corrupted files and out of date config objects.

Right now it succesfully detects a mismatch, but continues to throw
BinaryFormatter version incompatibility exceptions for all subsequent object
reads from the stream. I was hoping to just ignore the bad object and
continue loading the good ones. Is there a way to recover or reset the
BinaryFormatter so it will continue reading?

Here is the code I'm using to read in the configurations:

Dim formatter As IFormatter = New BinaryFormatter
Dim str As Stream = New FileStream("OBSOptions.bin",
FileMode.Open, FileAccess.Read, FileShare.None)
'Read each object in the configuration file
Do While (str.Position < str.Length)
Try
o = formatter.Deserialize(str)
'Load config objects
Select Case (o.GetType().Name)
Case GetType(ColorOptions).Name
_colorOpts = CType(o, ColorOptions)
Case GetType(GeneralOptions).Name
_generalOpts = CType(o, GeneralOptions)
Case GetType(DbOptions).Name
_dBOpts = CType(o, DbOptions)
End Select
Catch ex As Exception
End Try
Loop
str.Close()


Thanks,

Doug Lind

Panther Systems
 
D

Doug Lind

Clarification:

The first exception correctly indicates a misimatch between the object being
read and the target class definition. All subsequent exceptions reference
BinaryFormatter version incompatibility. It's as though the stream has
become corrupted...

Thanks
 
J

Jay B. Harlow [MVP - Outlook]

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top