BinaryFormatter deserialize gives SerializationException

前端 未结 6 959
别跟我提以往
别跟我提以往 2020-12-16 15:28

I\'m getting an:

System.Runtime.Serialization.SerializationException: Unable to find assembly \'myNameSpace, Version=1.0.0.0, Culture=neutral, Pub

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 16:02

    You can get around this issue without needing the DLL if you know the object...

    http://spazzarama.com/2009/06/25/binary-deserialize-unable-to-find-assembly/

    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.serializationbinder(VS.71).aspx

    Use the “System.Runtime.Serialization.SerializationBinder” class. By inheriting from this class it is possible to redirect all the requests for types from the binary formatter to the types of your choice.

    Here is a sample that will allow the types to be found in the current assembly regardless of which version of the assembly originally created the serialized stream:

    sealed class AllowAllAssemblyVersionsDeserializationBinder : System.Runtime.Serialization.SerializationBinder
    {
        public override Type BindToType(string assemblyName, string typeName)
        {     
            String currentAssembly = Assembly.GetExecutingAssembly().FullName;
    
            // In this case we are always using the current assembly
            assemblyName = currentAssembly;
    
            // Get the type using the typeName and assemblyName
            Type typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
                typeName, assemblyName));
    
            return typeToDeserialize;
        }
    }
    
    public static MyRequestObject Deserialize(byte[] b)
    {
        MyRequestObject mro = null;
        var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        using (var ms = new System.IO.MemoryStream(b))
        {
           // To prevent errors serializing between version number differences (e.g. Version 1 serializes, and Version 2 deserializes)
           formatter.Binder = new AllowAllAssemblyVersionsDeserializationBinder();
    
           // Allow the exceptions to bubble up
           // System.ArgumentNullException
           // System.Runtime.Serialization.SerializationException
           // System.Security.SecurityException
           mro = (MyRequestObject)formatter.Deserialize(ms);
           ms.Close();
           return mro;
        }
    }
    

提交回复
热议问题