Deserialization exception: Unable to find assembly
Asked Answered
N

2

12

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.

Nonpareil answered 26/10, 2012 at 16:38 Comment(1)
K... Context; should we assume you are using BinaryFormatter? The type you serialized: is that in an assembly that both projects are referencing? Are you accidentally serialising more data than you expect via events? (that happens really often)Secrest
S
14

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.

Secrest answered 26/10, 2012 at 16:47 Comment(4)
Yes, I use BinaryFormatter. I would like to go with your 1st option. So, I have to create a new project, and add them to both of the other two projcts? Am I right?Nonpareil
In the solution, add a new project - select "library". Now put the class there and remote it from the other two places. Finally, for both the original projects: references, add reference... select the new library project you just created.Secrest
This is what I just did! Thxa lot mate. Will let you know if it will work (but I know it will :)).Nonpareil
Works, but I got null for the class data. But this is now my issue to resalve it. Thx ones again.Nonpareil
B
2

All the assemblies containing classes in the class hierarchy of the object you are deserializing must be present in the application in which you are performing this deserialization. They could be either explicitly referenced (if you need compile-time safety with those classes) or only placed in the bin folder of the application so that they could be resolved at runtime. If they are not explicitly referenced you will have to use reflection in order to read the values from the deserialized instance.

Bede answered 26/10, 2012 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.