使用GZipStream问题进行序列化 [英] Serialization with GZipStream problem

查看:141
本文介绍了使用GZipStream问题进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些遗留代码将一些数据序列化到数据库并读取数据并对其进行反序列化。我知道反序列化工作正常,因为我可以读取数据库中的旧数据。但是,如果我保存新数据然后尝试读取它我得到一个SerializationException,二进制流'0'不包含有效的BinaryHeader。。我已经用序列化/反序列化代码创建了一些测试代码,并成功地重现了这个问题。我相信在序列化部分中意外更改了某些内容,导致它无法与反序列化部分一起使用。不幸的是,旧的代码在我们的源代码控制中不可用于比较,而编写它的人不再适用于公司。我怀疑序列化部分的gzip部分是错误的,但我无法弄清楚应该是什么。序列化/反序列化代码如下。行table =(Dictionary< int,>>>)bf.Deserialize(gz);是SerializationException发生的地方。我正在使用Visual Studio 2013和.NET 4.5。



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

// 为简洁起见,我遗漏了将数据加载到表格中的代码。
// 但是,我已确认数据已正确加载到表中。


#region serialize data

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

使用 var ms2 = new MemoryStream())
{
使用 var gz = new GZipStream(ms2,CompressionMode.Compress))
{
var data = ms1.GetBuffer();
gz.Write(data, 0 ,data.Length);
serializedData = ms2.GetBuffer();
}
}
}

#endregion序列化数据


#region deserialize data

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

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

#endregion反序列化数据

解决方案

我找到了问题的原因,它是微软。我在.NET 3.5中重新编译了测试代码,它工作正常。当我在.NET 4或4.5中编译它时它不起作用。这解释了为什么序列化/反序列化在.NET 3.5中编写的旧代码中起作用。因此,对于提供这种娱乐的微软而言,这也是一个秘密。 X |

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.

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

解决方案

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|


这篇关于使用GZipStream问题进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆