BinaryFormatter can't find 'Assembly' upon deserialize in c#

放肆的年华 提交于 2020-01-12 08:49:09

问题


I have a program that serializes and deserializes calls, and when I try to attach my DLL to another program, it says: Unable to find assembly 'ASCOM.BHOProxy.Connector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=74643865492aa2e6'.

I could understand if this was a reference problem or something, but the problem is that the code that throws the exception is in ASCOM.BHOProxy.Connector. I've thought of going with some kind of third party Serializer, but I'm not quite sure what to use. The assembly is loaded by another DLL which is loaded by the application.

The serialized data gets transmitted across a TCP connection to an identical connector (often the same file loaded by another program), where it is deserialized. The exception is thrown when it tries to deserialize it, but it only does it when this is called from an external program. It works fine when debugging in visual studio.

Their Program --(late binding)--> My Main DLL --(.NET Project Reference)--> My Connector DLL

Stacktrace:

   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at Connector.PortComProxy.DecodeMessage(List`1 buff) in c:\Users\Arlen\Documents\Visual Studio 2012\Projects\DriverProxy\PortComClient\PortComProxy.cs:line 259

回答1:


I can't say why the assembly isn't found sometimes. However I've used the AppDomain.AssemblyResolve event to load assemblies that couldn't be found via the normal assembly load resolution provided by .NET. In my case it was because I had to find the assembly from a registry entry, using the event I was able to find and load the assembly preventing the Assembly not found exception.

At the very least tapping into this event might allow you to verify what type the BinaryFormatter is trying to resolve.




回答2:


Thank you very much Ken, that did it. Here is what I did for anyone else who might need it. I don't know if it makes any difference whether the resolver is static or not.

using System.Reflection;
...
public class MyClass{
    public MyClass()
    { 
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
    }
    private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        return typeof(MyClass).Assembly;
    }
}


来源:https://stackoverflow.com/questions/13821198/binaryformatter-cant-find-assembly-upon-deserialize-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!