Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialization exception: Unable to find assembly

I'm serializing some data like fields and custom class to create a binary data (byte array).

Then I want to Deserialize it back from binary data to fields and class.

But I get an exception. It would all work fine if these two methods would happen in same assembly - but its not.

I do Serialization in one assambly, and do the Deserialization in another one. And this is the excaption saying too: Unable to find assembly 'MyAssamblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

NOTE 1: I have no issues with getting the fields back, only the classes causes them.

NOTE 2: I have this same class in both assemblies.

like image 521
Mitja Bonca Avatar asked Oct 26 '12 16:10

Mitja Bonca


1 Answers

NOTE 2: I have this same class in both assemblies

No you don't. At least, not as far as the runtime is concerned. You have two different types that happen to have the same name. A type is defined by its assembly. Thus "SomeType in AssemblyA" is completely different to "SomeType in AssemblyB", even if they happen to have been compiled from the same source file.

BinaryFormatter works with type information, so this won't work. One option would be to move the type to a library dll that both the other projects reference - then it is only defined once, and it will be happy.

Another option is to work with a contract-based serializer (rather than a type-based serializer). This means that "classes that look similar enough" are fine, even if they are in different assemblies (and perhaps have different source, as long as it is "similar enough"). Examples of suitable serializers for this would include (plus a few others) XmlSerializer, DataContractSerializer (but not NetDataContractSerializer), JavaScriptSerializer, or protobuf-net if you want dense raw binary.

like image 107
Marc Gravell Avatar answered Sep 17 '22 15:09

Marc Gravell