Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net-4.0

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
like image 420
Arlen Beiler Avatar asked Dec 11 '12 13:12

Arlen Beiler


2 Answers

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.

like image 198
Ken Henderson Avatar answered Nov 17 '22 09:11

Ken Henderson


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;
    }
}
like image 34
Arlen Beiler Avatar answered Nov 17 '22 09:11

Arlen Beiler