I have a C# solution with a referenced dll (also C# with the same .Net version). When I build the solution and run the resulting exe, without merging the exe and the referenced dll, everything works fine.
Now I want to merge these into one exe. I run ILMerge and everything appears to work ok. I try to execute the exe and it seems to run just fine until it tries to deserialize an object defined in the referenced dll.
using (Stream fstream = new FileStream(file_path, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(fstream) as ControlledRuleCollection;
// throws unable to find assembly exception
}
Is there maybe some ILMerge option I'm missing here?
SerializationBinder was also my solution. But I have the class in a DLL which is referenced. So i have to search in all load assemblies. I have modified the answers bevor with the parameter if the binder should search in dlls.
Usage:
In case you merge assemblies into a existing one (for-example all DLLs to the EXE) you can use the solution proposed in this answer:
This at least works for deserializing pre-merge. IL-Merge also still passes; even if you can't compile with a type-forward to a type of the same assembly...
I have not tried, if serializing works post-merge yet. But I'll keep my answer updated.
For anyone having this issue, trying to deserialize from a different assembly, I found this solution which seems to great for me using a small "BindChanger" class with a shared namespace for the Object type in question. https://www.daniweb.com/programming/software-development/threads/339638/deserializing-in-a-different-assembly
It sounds like you've serialized an object inside a DLL, then merged all of the assemblies with ILMerge and are now trying to deserialize that object. This simply won't work. The deserialization process for binary serialization will attempt to load the object's type from the original DLL. This DLL doesn't exist post ILMerge and hence the deserialization will fail.
The serialization and deserialization process need to both operate pre or post merge. It can't be mixed
You can do this by creating and adding a SerializationBinder sub class that will change the assembly name before the deserialization happens.
Then when deserializating add this to the BinaryFormatter:
I had a situation where serialized data was being stored in SQL server by an old .NET service that had been in place for years. I needed to get the data out of SQL and ran into this also. I was able to refer to the .exe and make it work until I used the solution mentioned above. However my assembly names were different.