473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Serialization and BinaryFormatter version incompatibility exceptions

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
2 5791
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dominic | last post by:
Hi everybody, I'm planning to use serialization to persist an object (and possibly its child objects) in my application. However, I'm concerned about the backward compatibility issue. I'm...
2
by: Mike | last post by:
Hello. I am using C# in .NET v1.1. I am trying to serialize an object to disk and read it back again. The serialization seems to work OK (at least, it completes without error). However, I get...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
4
by: Alexander | last post by:
There was such problem, At compilation with SP1 (dotnetfx11SP1) On computers without him{it} недесериализуются some objects. For example (System. Collections. Comparer) and together with...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
11
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
9
by: norvinl | last post by:
Hi, I'm serializing a class and using shared memory / deserialization to send it to other processes. I can serialize with one app and deserialize with another instance of the same app. But...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.