Reputation: 3685
I have a strange situation in regards to xml serialization...
If I run MyApp.exe (.NET 2.0 WinForms app) with properly generated MyApp.XMLSerializers.dll all is well and the serialization is fast (no serialization assemblies are generated at runtime, because serializers dll is found and is behaving).
Now, if I embed MyApp.exe as a resource in MyOtherManagedApp.exe (also .net 2.0) and execute the original app from inside as follows...
pasm = System.Reflection.Assembly.Load(MyOtherManagedApp.Properties.Resources.MyAppExeBinary);
Type type = pasm.GetType("MyApp.MyModule");
type.InvokeMember("Main", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[] {args});
... the original app loads and runs just fine, except for serialization part:
If MyApp.XMLSerializers.dll is present in the working directory of MyOtherManagedApp, I get an error stating that MyApp.exe assembly cannot be found (the error is thrown by autogenerated MyApp.XMLSerializers.dll which for some strange reason, inspite of the fact that not only MyApp assembly has been loaded but is in fact executing, fails to find it).
If MyApp.XMLSerializers.dll is NOT present in the working directory, no errors occur, but serialization assemblies are now being generated at run time which result in a big performance hit.
So, my question is why does it not work as it should? Namely, why MyApp.XMLSerializers.dll works perfectly if the serialization is started by MyApp.exe when it's running by itself; whereas if it was started via Assembly.Load and InvokeMember from a different assembly, MyApp.XMLSerializers.dll complaints that it cannot find the very same MyApp assembly which has been dynamically loaded and is now running?
Upvotes: 2
Views: 1205
Reputation: 3685
I found the solution for this specific problem. The solution is to handle AppDomain.CurrentDomain.AssemblyResolve event for BOTH MyApp.XMLSerializers.dll AND MyApp.exe (the one which is embedded as a resource) INSIDE MyApp.exe!
If e.Name.StartsWith(XMLSerializersAssemblyName) Then 'MyApp.XMLSerializers.dll lookup
Return Assembly.LoadFile(MyOtherManagedApp_EXEFolder + "\" + XMLSerializersAssemblyName + ".dll")
ElseIf e.Name = Assembly.GetExecutingAssembly.FullName Then 'MyApp.exe lookup
Return Assembly.GetExecutingAssembly
End If
This way MyApp.XMLSerializers.dll is found and is properly loaded, and more importantly the MyApp.XMLSerializers.dll can the find the embedded MyApp.exe (which is not present anywhere as a file).
Upvotes: 2