Reference to a loaded Assembly
I'm not sure how to describe this best. But I have a problem understanding the load process of Assemblies.
My application uses plug-ins via Reflection. It works pretty fine and I'm quiet happy with that. Now I've stumbled upon a problem which confuses me and I think I missed something: In one of my modules, I reference another module. At run time all modules are loaded. There are module ClientManager and the calling module Calculations. ClientManager and Calculations are both loaded. Calculations references ClientManager . When Calculations tries to load a class of ClientManager I get a File Not Found-exception. Both assemblies are loaded from a bytestream in memory (via Assembly.Load(byte[]). When Calculations tries to load the class of ClientManager this is how it looks like:loaded: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
many more Assemblies... loaded: ClientManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null loaded: Calculations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Name of Assembly to be loaded: ClientManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Requested from: Calculations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
So, the Assembly is loaded, yet it gets requested and the request fails. What am I missing? Do I have开发者_开发百科 to load the assembly twice?
I'm grateful for any help.
Greetings,
SkalliYour problem looks very similar to this one I've encountered developing a plug in: Where does Visual Studio look for assemblies?.
I think that you should first of all understand where .NET is looking for your assembly and compare it with the one which is already loaded in your AppDomain. This can be done using ProcMon.exe to see where your app is not able to find the assembly and looking at the CodeBase property of the ClientManager that you can find in AppDomain.CurrentDomain.GetAssemblies().
I imagine that these 2 paths will be different, but it's difficult to imagine why your app is looking for assemblies in different places without knowing them.
In the end I solved my problem using the AssemblyResolve event, too, simply looking for my assembly in the currently loaded assemblies and just retutning it (without loading it again).
This is how I did it. I'm not so sure that it is really neat, since it works only because the assembly that was not found was already loaded:
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
foreach (Assembly anAssembly in AppDomain.CurrentDomain.GetAssemblies())
if (anAssembly.FullName == args.Name)
return anAssembly;
return null;
}
精彩评论