在c#中反序列化时,BinaryFormatter找不到'汇编'

问题描述 投票:4回答:2

我有一个序列化和反序列化调用的程序,当我尝试将我的DLL附加到另一个程序时,它说:Unable to find assembly 'ASCOM.BHOProxy.Connector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=74643865492aa2e6'.

我可以理解这是一个参考问题还是什么,但问题是抛出异常的代码是在ASCOM.BHOProxy.Connector中。我曾想过要使用某种第三方Serializer,但我不太清楚要使用什么。程序集由另一个由应用程序加载的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
c# .net-4.0
2个回答
5
投票

我不知道为什么有时候找不到集会。但是,我使用AppDomain.AssemblyResolve事件来加载通过.NET提供的正常程序集负载分辨率无法找到的程序集。在我的情况下,这是因为我必须从注册表项中找到程序集,使用我能够找到的事件并加载程序集,防止程序集未找到异常。

至少利用此事件可能允许您验证BinaryFormatter尝试解析的类型。


3
投票

非常感谢Ken,做到了。这是我为其他可能需要它的人所做的。我不知道旋转变压器是否静止是否有任何区别。

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.