BinaryFormatter cannot find "Assembly" when deserializing in C #

I have a program that serializes and deserializes calls, and when I try to connect 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 problem was a link or something like that, but the problem is that the code that throws the exception is in ASCOM.BHOProxy.Connector . I was thinking about going with some kind of third-party Serializer, but I'm not quite sure what to use. The assembly is loaded by another DLL loaded by the application.

Serialized data is transferred over a TCP connection to an identical connector (often the same file downloaded by another program), where it is deserialized. An exception occurs when he tries to deserialize it, but this only happens when it is called from an external program. It works great when debugging in a 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 
+4
source share
2 answers

I can’t say why the assembly sometimes does not occur. However, I used the AppDomain.AssemblyResolve event to load assemblies that could not be found using the standard assembly loading permission provided by .NET. In my case, this was due to the fact that I had to find the assembly from the registry entry using an event that I could find and load the assembly, preventing the assembly from finding an exception.

At the very least, clicking on this event may allow you to check what type of BinaryFormatter is trying to solve.

+5
source

Thanks so much Ken did this. Here is what I did for those you might need. I do not know if it matters 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; } } 
+3
source

All Articles