反序列化程序集版本问题 - 无法加载文件或程序集 [英] Deserialisation Assembly Version Issue - Could not load file or assembly

查看:84
本文介绍了反序列化程序集版本问题 - 无法加载文件或程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个内置于C#targetting  .Net 2.0的应用程序。我们的一个库中定义的类型使用BinaryFormatter序列化,压缩并存储在数据库中。当我们尝试使用稍后版本的程序集反序列化对象时,反序列化会失败,因为格式化程序正在尝试加载以前版本的程序集。即使类成员在版本之间没有更改,也会发生这种情况。

We have an application built in C# targetting .Net 2.0. A type defined in one of our libraries is serialized using a BinaryFormatter, compressed and stored in a database. When we attempt to deserialize the object using a later build of our assembly, the deserialization fails because the formatter is attempting to load the previous version of the assembly. This occurs even though the class members have not changed between versions.

奇怪的是,这种行为在XP和Vista上是一致的,但在Windows 7上不会发生!例如,如果我们使用build xxxx在运行Vista或XP的计算机上运行应用程序并将数据保存到数据库,我们可以使用更高版本的yyyy成功反序列化Windows 7计算机上的数据,但无法反序列化原始数据机器或运行Vista或XP的任何其他机器。

Strangely, this behaviour is consistent on XP and Vista but does not occur on Windows 7! For example, if we run the application on a machine running Vista or XP using build xxxx and save data to the database we can successfully deserialize the data on a Windows 7 machine using a later build yyyy but are unable to deserialize the data on the original machine or any other machine running Vista or XP.

任何人都可以建议为什么会发生这种情况?有没有办法强制格式化程序使用当前程序集而不是尝试加载thae缺少,早期的汇编?

Can anyone suggest why this is happening? Is there any way to force the formatter to use the current assembly instead of attempting to load thae missing, earlier assembly?

谢谢。

ps值得注意的是,我们所有的程序集都有很强的名称。

p.s. It might be worth noting that all our assemblies are strongly named.

推荐答案

我相信这实际上是因为它是一个来自强名称汇编的类型。

I believe this is actually because it is a type from a strongly named assembly.

你可以通过使用SerializationBinder的实现来强制执行代码 -

You can force a rediction in code - by using an implementation of SerializationBinder

EG - 只是指出你正确的方向.... 这是我在第三方组件供应商更改包含序列化类型的程序集的全名时使用的代码的一部分。 在你的实例中,你有强大的名字
你的程序集 - 强名称在版本之间发生了变化 - 原则上也是如此。

E.g. - just to point you in the right direction....  This is a portion of code I used when a third party component vendor changed the full name of the assembly that contained serialized types in.  In your instance the fact you have strong named your assembly - the strong name has changed between version - so same thing in principle.

class MyDeSerializationBinder:SerializationBinder

class MyDeSerializationBinder : SerializationBinder

{

public override Type BindToType(string assemblyName,string typeName)

        {

          



            //将类型选择的控制委托给默认的活页夹(mscorlib中的内部代码),


            // 此方法应返回null。



$


             //从NMath代码2.2.1升级到4.0.0。这改变了一些序列化的汇编名称

            // 类型包括,从NMathCore,Version = 2.2.1.0到NMathShared,Version = 4.0.0.0

            // 从旧的NMathCore程序集中仅序列化了3种类型。


            //  FloatVector,FloatComplex& FloatComplexVector。

            //只需将所有类型从旧装配序列化为新装配。

            // 这样 - 它也总是有效 - 因为在编译时强烈打字。

           如果(的AssemblyName == @" NMathCore,版本= 2.2.1.0,文化=中性公钥= 0ece19bef78ff024")

        &NBSP ;   {

                switch(typeName)

                {

                   情况下" CenterSpace.NMath.Core.FloatComplexVector" ;:

                        返回的typeof(CenterSpace.NMath.Core.FloatComplexVector);

                    情况下" CenterSpace.NMath.Core.FloatComplex" ;:

                         return typeof(CenterSpace.NMath.Core.FloatComplex);

                    情况下" CenterSpace.NMath.Core.FloatVector" ;:

                        返回的typeof(CenterSpace.NMath.Core.FloatVector);

                    默认值:

                        返回null;

                }


           返回null;



        }

public override Type BindToType(string assemblyName, string typeName)
        {
          

            // to delegate control of the type selection to the default binder (internal code in mscorlib),
            //  this method should return null.



            // Upgraded from NMath code 2.2.1 to 4.0.0. This changed the assembly names that some serialized
            //  types were in, from NMathCore, Version=2.2.1.0 to NMathShared, Version=4.0.0.0
            //  There were only 3 types that were serialized from the old NMathCore assembly.
            //  FloatVector, FloatComplex & FloatComplexVector.
            // Just point all types serialized from old assembly to new assembly.
            //  This way - it'll also always work - because of the strong typing at compile time.
            if (assemblyName == @"NMathCore, Version=2.2.1.0, Culture=neutral, PublicKeyToken=0ece19bef78ff024")
            {
                switch (typeName)
                {
                    case "CenterSpace.NMath.Core.FloatComplexVector":
                        return typeof(CenterSpace.NMath.Core.FloatComplexVector);
                    case "CenterSpace.NMath.Core.FloatComplex":
                        return typeof(CenterSpace.NMath.Core.FloatComplex);
                    case "CenterSpace.NMath.Core.FloatVector":
                        return typeof(CenterSpace.NMath.Core.FloatVector);
                    default:
                        return null;
                }

            return null;

        }

}


这篇关于反序列化程序集版本问题 - 无法加载文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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