By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,032 Members | 1,163 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,032 IT Pros & Developers. It's quick & easy.

Serialization and BinaryFormatter version incompatibility exceptions

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Doug Lind" <dl***@panthersys.com> wrote in message
news:vm************@corp.supernews.com...
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

Nov 20 '05 #2

P: n/a
Doug,
You need to create a SerializationBinder that maps versions of the original
objects to current versions. Also you may need to implement ISerializable so
you can pick & choose which fields to are available from the serialization
stream.

For details on Serialization see:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay

"Doug Lind" <dl***@panthersys.com> wrote in message
news:vm************@corp.supernews.com...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.