我有一个序列化和反序列化调用的程序,当我尝试将我的 DLL 附加到另一个程序时,它说:Unable to find assembly 'ASCOM.BHOProxy.Connector, Version=1.0.0.0, Culture=中性,PublicKeyToken=74643865492aa2e6'。
我能理解这是不是引用问题或其他问题,但问题是引发异常的代码在 ASCOM.BHOProxy.Connector
中。我想过使用某种第三方序列化程序,但我不太确定该使用什么。该程序集由应用程序加载的另一个 DLL 加载。
序列化的数据通过 TCP 连接传输到相同的连接器(通常是另一个程序加载的相同文件),在那里它被反序列化。当它试图反序列化它时抛出异常,但它只在从外部程序调用时才会这样做。在 visual studio 中调试时它工作正常。
Their Program --(late binding)--> My Main DLL --(.NET Project Reference)--> My Connector DLL
堆栈跟踪:
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
最佳答案
我不能说为什么有时找不到程序集。但是我用过 AppDomain.AssemblyResolve事件加载无法通过 .NET 提供的正常程序集加载解析找到的程序集。在我的例子中,这是因为我必须从注册表项中找到程序集,使用我能够找到并加载程序集的事件来防止程序集未找到异常。
至少利用此事件可以让您验证 BinaryFormatter 试图解析的类型。
关于c# - BinaryFormatter 在 C# 中反序列化时找不到 'Assembly',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821198/