XMLSerializers Dll failed with Assembly.Load setting (byte [] asm)

I have a weird situation regarding xml serialization ...

If I run MyApp.exe application (.NET 2.0 WinForms application) with correctly generated MyApp.XMLSerializers.dll file, everything will be fine and serialization will be fast (no assembly assemblies are generated during runtime because serializers dlls are found and behave) ...

Now if I insert MyApp.exe as a resource into MyOtherManagedApp.exe (also .net 2.0) and execute the original application from the inside like this:

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 the serialization part:

  • If MyApp.XMLSerializers.dll is present in the working directory of MyOtherManagedApp, an error appears indicating that the assembly MyApp.exe cannot be found (the error is caused by the auto-generated MyApp.XMLSerializers.dll, which for some strange reason, despite the fact that not only assembly MyApp has been loaded, but is actually running, it cannot be found).

  • If MyApp.XMLSerializers.dll is NOT present in the working directory, no errors are thrown, but assembly assemblies are now generated at runtime, which has a great effect.

So my question is, why isn't it working the way it should? Namely why MyApp.XMLSerializers.dll works fine if serialization is started by MyApp.exe when it starts; and if it was launched via Assembly.Load and InvokeMember from another assembly, MyApp.XMLSerializers.dll complains that it cannot find the same assembly MyApp that was dynamically loaded and is now running?

+3


source to share


1 answer


I found a solution to this specific problem. The solution is to handle the AppDomain.CurrentDomain.AssemblyResolve event for BOTH MyApp.XMLSerializers.dll AND MyApp.exe (the one that 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, the MyApp.XMLSerializers.dll file is found and loaded correctly, and more importantly, MyApp.XMLSerializers.dll can find the built-in MyApp.exe file (which is not present anywhere in the file).

+3


source







All Articles