Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with some legacy code that serializes some data to a database and reads the data back and deserializes it. I know the deserialization works correctly because I can read the old data in the database. However, if I save new data then try to read it back I get a SerializationException, "Binary stream '0' does not contain a valid BinaryHeader.". I have created some test code with just the serialization/deserialization code and have successfully reproduced the problem. I believe something was accidentally changed in the serialization section to cause it to not work with the deserialization section. Unfortunately, the old code is not available in our source control for comparison and the person who wrote it no longer works for the company. I suspect the gzip part of the serialization section is wrong, but I can’t figure out what it should be. The serialization/deserialization code is below. The line "table = (Dictionary<int,>>>)bf.Deserialize(gz);" is where the SerializationException occurs. I am using Visual Studio 2013 and .NET 4.5.

C#
Dictionary<int, Dictionary<double, List<double>>> Table;

//For brevity I have left out the code for loading data into Table.
//However, I have confirmed that the data is being correctly loaded into Table.


            #region serialize data

            System.Data.Linq.Binary serializedData;
            using (var ms1 = new MemoryStream())
            {
                var bf = new BinaryFormatter();
                bf.Serialize(ms1, Table);

                using (var ms2 = new MemoryStream())
                {
                    using (var gz = new GZipStream(ms2, CompressionMode.Compress))
                    {
                        var data = ms1.GetBuffer();
                        gz.Write(data, 0, data.Length);
                        serializedData = ms2.GetBuffer();
                    }
                }
            }
 
            #endregion serialize data


            #region deserialize data

            Dictionary<int, Dictionary<double, List<double>>> table;

            using (var ms = new MemoryStream(serializedData.ToArray()))
            {
                using (var gz = new GZipStream(ms, CompressionMode.Decompress))
                {
                    var bf = new BinaryFormatter();
                    table = (Dictionary<int, Dictionary<double, List<double>>>)bf.Deserialize(gz);
                }
            }

            #endregion deserialize data
Posted
Updated 17-Oct-14 6:03am
v2
Comments
BillWoodruff 17-Oct-14 12:16pm    
Is it the case that the previous software in use could serialize and compress, and then decompress and de-serialize a Generic Dictionary whose Value Type is a Generic Dictionary ?

Can you compare a non-compressed file created with the original software with a file created containing only the pre-compressed data of your current solution ?
BillW33 17-Oct-14 12:26pm    
The previous software could serialize and compress, and then decompress and de-serialize the Dictionary<int, dictionary<double,="" list<double="">>> Table. The original software saves the data to a database, not a file. However, I have checked the non-compressed data of the original program and compared it to the non-compressed data of the current program and the starting data is the same.
BillWoodruff 17-Oct-14 13:21pm    
imho, this raises the question if GZip can handle such a Dictionary.

1 solution

I have found the cause of the problem and it is Microsoft. I recompiled the test code in .NET 3.5 and it works fine. When I compile it in .NET 4 or 4.5 it doesn't work. That explains why serialization/deserialization worked in the old code which was written in .NET 3.5. So thanx and a tip o'the hat to Microsoft for providing this entertainment. X|
 
Share this answer
 
v2
Comments
BillWoodruff 17-Oct-14 14:46pm    
Good for you ! I suspect other people are having a similar problem:

http://stackoverflow.com/questions/9806381/gzipstream-machine-dependence

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900