Symptoms
Suppose you call Assembly.LoadFrom (...) to load an assembly named SimpleAsm in which type SimpleType is defined. Then you try to serialize a SimpleType ojbect, it works fine. But as soon as you deserialize some SimpleType object, you encounter following Exception:
System.Runtime.Serialization.SerializationException: Cannot Find the assembly SimpleAsm, Version = xxxx, Culture = xxxx, publickeytoken = xxxx.
Even the call to deserialization is in SimpleAsm Itselft, The Exception STISTS. Although It Looks a Little Bit Ridiculous, It's The Proper Behavior by Design.
Cause
"Some extensible applications use Assembly.LoadFrom to load an assembly and then construct objects from types defined in the loaded assembly. These objects can be serialized to a stream without any trouble. However, when deserializing this stream, the formatter attempts to load the assembly by calling Assembly's Load or LoadWithPartialName method instead of calling the LoadFrom method. In most cases, the Common Language Runtime (CLR) will not be able to locate the assembly file, causing a SerializationException exception to be thrown. "
Resolution
Implement and register a system.resolveeventhandler to help clr class loader redirect to the profoad it Into memory. The Handler May Look Like this:
public static Assembly MyResolveEventHandler (object sender, ResolveEventArgs args) {string [] strProps = args.Name.Split (new char [] { ','}); String strPath = m_codebase strProps [0] ".dll"; return AskEMBLY.LOADFROM (STRPATH);
m_codebase is a relative directory that your assembly resides in Before deserialize any SimpleType objects, register your event handler to the application domain:.. Thread.GetDomain () AssemblyResolve = resolveHandler;
Also Remember To Unregister The Handler When Finish Deserializing SimpleType Objects To Avoid Any Side-Effects That May Be Brought by Custom Event Handler:
Thread.getdomain (). AssemblyResolve - = resolvehandler;
More information
.NET 247: SerializationException on Microsoft.public.vsNet.Iderun-Time Serialization