views:

85

answers:

2

Hello,

In our .NET application we depend on binary serialization for our application project files. The main project class file uses the following method to serialize itself (note all exceptionhandling is removed for clarity).

public void ProjectSerialize(string filename)
    {
        Stream s = File.Open(filename, FileMode.Create);
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(s, this);
    }

The class has many methods and properties that are stored. Now everything works fine, however when we want to release a new version of the application that has a new version of the project class, we are not compatible anymore with earlier versions. I know it is a design error, since of course i had to use my own defined serialization and I should have not depend on .NET for this important type of files.

Now my question, how can I read my older serialized project files? Is there a format I should know, how did .NET serialize in the first place? I just want my customers to be compatible with the older fileformat, but I don't now how to write a file converter for the old stored project files.

Thanks,

Erik

+1  A: 

Do you have the old version of your project in source control?

If so, I recommend that you use that to load the existing data, then write it out in a more version-friendly fashion that your new code can successfully read.

Btw, you should really close the stream to make sure all the data is flushed:

public void ProjectSerialize(string filename)
{
    using (Stream s = File.Open(filename, FileMode.Create))
    {
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(s, this);
    }
}
Jon Skeet
Hi Jon,Thanks for the comment and of course I close the stream. The problem is that the application is already running at customers and it is impossible to get all files from them. My first idea was to make a small converter that converts older project files to new controlled files. However I found that only an application with the exact same name as the original application can read the files.
Enrico
It shouldn't need to be an application with the exact same name, but probably read from an *assembly* with the same name. So you could create a new executable which refers to a class library with the same name as it had before. Ship it in a different subdirectory as a conversion tool.
Jon Skeet
+1  A: 

A bit late now, but perhaps you should read up on Version Tolerant Serialization?

RichardOD
+1, this will restore the fields of the older versions while helping you to prevent that exceptions are thrown. Mind that only the data in your older classes is serialized, not its methods.
Dabblernl
I am just reading this, this should do the trick!! Thanks a lot RichardOD
Enrico
wcf data contracts or google protocol buffers (Jon or Marcs) are a much more robust solution.
Sam Saffron
@Sam- protocol buffers look like a good solution, but the code in this case is already in place.
RichardOD