执行反序列化时无法找到程序集异常 [英] Unable to find assembly exception,when perform deserialization

查看:589
本文介绍了执行反序列化时无法找到程序集异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winforms应用程序具有一个自定义控件,其属性 LayoutStream之一是 byte [] 类型,并且是可序列化的属性。目前,我已将应用程序从4.6框架移植到NetCore 3.0项目。

My winforms application has a custom control, one of its property "LayoutStream" is byte[] type and it is serializable property. Currently I have ported my application from 4.6 framework to NetCore 3.0 project.

在运行移植的NetCore 3.0项目时,在反序列化resx文件中的数据时,即时消息出现异常。

While run ported NetCore 3.0 project, im getting below exception when deserialize the data from resx file.

this.control1.LayoutStream = ((SerializableByteList)(resources.GetObject("control1.LayoutStream")));


    public SerializableByteList LayoutStream
    {
        get
        {
            return LayoutStream;
        }
        set
        {
            LayoutStream= value;
            if (value != null)
                this.stmLayout.Write(value.Bytes.ToArray(), 0, value.Bytes.ToArray().Length);
        }
    }

    [Serializable]
    public class SerializableByteList 
    {
        public SerializableByteList()
        {
            Bytes = new List<byte>();
        }
        public List<byte> Bytes { get; set; }
    }




例外发生:
例外抛出:System.Runtime.Serialization.Formatters.dll
中的 System.Runtime.Serialization.SerializationException System.Runtime.Serialization.Formatters.dll中发生了类型为 System.Runtime.Serialization.SerializationException的未处理异常
无法找到程序集'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'

Exception Occurs: Exception thrown: 'System.Runtime.Serialization.SerializationException' in System.Runtime.Serialization.Formatters.dll An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.Formatters.dll Unable to find assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

请提出解决方案。

推荐答案

BinaryFormatter 不可移植;它是为在运行相同代码的两个节点之间进行远程处理而设计的,而不是在框架之间持久保存数据。所以:我的 main 建议很简单:

Fundamentally, BinaryFormatter is not portable; it was designed for remoting between two nodes running identical code, not persisting data between frameworks. So: my main advice is simply:


不要使用 BinaryFormatter

我意识到这条船可能已经航行了,但是非常实用的选择是:

I realise this boat may have sailed, but a very pragmatic option is to:


  • 选择一个新的序列化器-确保它是可移植的(json,xml,protobuf等);几乎在您的旧代码中 BinaryFormatter

  • 以外的任何内容,使用当前代码反序列化数据,以及在新代码中使用新的序列化程序

  • 分别对其进行序列化,仅以新格式反序列化它。

  • pick a new serializer - making sure it is portable (json, xml, protobuf, etc); just about anything except BinaryFormatter
  • in your old code, deserialize the data with your current code, and serialize it separately with the new choice of serializer
  • in your new code, deserialize it in the new format only

push 上,您可以尝试使用自定义的 SerializationBinder ;这里的想法是您将 SerializationBinder 子类化,重写方法,并告诉它在哪里可以找到东西。这里的根本问题是:

At a push, however, you could try playing with a custom SerializationBinder; the idea here is that you subclass SerializationBinder, override the methods, and tell it where to find things. The fundamental problem here is that:


  1. 程序集(mscorlib等)已更改标识

  2. 类型在很多情况下已经在程序集之间移动了

  1. the assemblies (mscorlib etc) have changed identity
  2. types have moved between assemblies in many cases

这两个问题都需要处理。

Both of those problems need handling.

部分代码:

var bf = new BinaryFormatter { Binder = MyBinder.Instance };
// etc

with

class MyBinder : SerializationBinder
{
    private MyBinder() { }
    public static MyBinder Instance { get; } = new MyBinder();
    public override Type BindToType(string assemblyName, string typeName)
    {
        // TODO: check assemblyName and typeName against some known list, and
        // return the Type that should be used, often *instead of* the
        // one it expected;
    }
    public override void BindToName(Type serializedType,
        out string assemblyName, out string typeName)
    {
        //TODO: the opposite
    }
}






但这很辛苦,和荒谬易碎。将您的时间花在移植到其他序列化程序IMO上的时间大大多了。乐意根据您的需求推荐一些。


But this is hard work, and absurdly brittle. Your time would be much better spent porting to a different serializer, IMO. Happy to recommend some, depending on your needs.

侧面说明:他们已经反复尝试了 杀死.NET Core的 BinaryFormatter ,可惜它幸免了。

Side note: they have tried repeatedly to kill BinaryFormatter for .NET Core, but it sadly survived.

这篇关于执行反序列化时无法找到程序集异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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