二进制流"0"在反序列化时不包含有效的BinaryHeader错误 [英] Binary stream '0' does not contain a valid BinaryHeader error on deserialization

查看:1693
本文介绍了二进制流"0"在反序列化时不包含有效的BinaryHeader错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去两天里找到了该问题的答案之后,我希望这里的人能为您提供帮助.

After searching for an answer to this issue for the last 2 days, I'm hoping someone here can help.

我已经使用VS2012用c#编写了一个程序,该程序使用BinaryFormatter将用户可序列化的类序列化为Stream,然后再将其保存到文件中,从而保存用户的项目数据.该程序已经使用了一段时间,但是最近用户无法打开他前一天保存的文件.他向我发送了文件,而我在防盗器中遇到的错误是:

I have written a program in c# using VS2012 that saves the user's project data using BinaryFormatter to serialize a serializable class to a Stream before saving it to a file. The programme has been in use for some time, however recently a user couldn't open a file he saved the day before. He sent me the file, and the error I get in the degugger is:

二进制流'0'不包含有效的BinaryHeader.可能的原因是在序列化和反序列化之间无效的流或对象版本更改."

"Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."

鉴于用户在前一天保存了数据,序列化和反序列化之间的基础对象结构或格式没有任何变化.

Given the user saved the data the day before, there was no change to the underlying object structure or format between serialization and deserialization.

这个问题以前从未出现过,使我相信它必须是间歇性的,因此对对象进行序列化和反序列化没有明显的问题.

This problem has not come up before, leading me to believe it must be very intermittent, and therefore there is no obvious problem with the objects being serialized and deserialized.

我的问题是:

我对这个错误的理解是,序列化数据的格式与要反序列化的对象的格式不匹配.这样对吗?还有其他原因吗?

My understanding of this error is that the format of the serialized data does not match the format of the object being deserialized into. Is this correct? Are there any other causes?

如果是这样,怎么可能导致这种间歇性错误?

If so, what could cause this to be such an intermittent error?

即使BinaryFormatter认为格式不正确,是否有任何方法可以从该文件中检索数据?

Is there any way to retrieve the data from this file, even though the BinaryFormatter doesn't think it's in the correct format?

是否有更安全的方式保存和加载数据?我已经看到XmlFormatter似乎是一个更好的选择,但是如何确保保存和调用的数据的完整性呢?

Is there a safer way to save and load data? I've seen that XmlFormatter seems to be a better choice, but how will this help with ensuring the integrity of the data being saved and recalled?

如果有帮助,我用来序列化/反序列化的代码如下:

If it helps, the code I am using to serialize/ deserialize is as follows:

//serialize

SEProjectData serializedProject = serializeProjectData();

Stream stream = File.Open(saveFileDialog1.FileName, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, serializedProject);
stream.Close();

//deserialize

Stream stream = File.Open(path, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();

stream.Seek(0, SeekOrigin.Begin);

SEProjectData projectData = (SEProjectData)bFormatter.Deserialize(stream);
stream.Close();

推荐答案

是的,BinaryFormatter可能是不兼容版本的(特别是如果您更改了字段),但这通常会引发另一个错误.您显示的代码或多或少都很好(我将使用"using"来确保在出现异常时立即关闭).没有复制就很难发表评论-我在想文件损坏".你有文件吗?可能是零长度?除了文件系统以外,它是否曾经被转移或存储过?有多种方法来弄乱文件内容(请参见 http://marcgravell.blogspot.com/2013/02/how-many-ways-can-you-mes-ups-io.html ).

Yes, BinaryFormatter can be version intolerant (especially if you change the fields), but that usually raises a different error. The code you show is more or less fine (I'd use "using" to ensure prompt closure in exception). Without a repro it is hard to comment - I'm thinking "damaged file". Do you have the file? Is it perhaps zero-length? Was it ever transferred or stored other than on a file system? There are any number of ways to mess up file contents (see http://marcgravell.blogspot.com/2013/02/how-many-ways-can-you-mess-up-io.html).

如果您想解决版本控制问题,我倾向于protobuf-net,但我有很大的偏见.不过,它对我们来说异常有效.但这听起来并不像通常的版本控制问题.

If you want to solve the versioning issue, I'm partial to protobuf-net, but I'm very biased. It works exceptionally well for us though. But this doesn't sound quite like the usual versioning problem.

这篇关于二进制流"0"在反序列化时不包含有效的BinaryHeader错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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