简体   繁体   中英

Loading assemblies and its dependencies

My application dynamically loads assemblies at runtime from specific subfolders. These assemblies are compiled with dependencies to other assemblies. The runtime trys to load these from the application directory. But I want to put them into the modules directory.

Is there a way to tell the runtime that the dlls are in a seperate subfolder?

One nice approach I've used lately is to add an event handler for the AppDomain's AssemblyResolve event.

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

Then in the event handler method you can load the assembly that was attempted to be resolved using one of the Assembly.Load, Assembly.LoadFrom overrides and return it from the method.

EDIT:

Based on your additional information I think using the technique above, specifically resolving the references to an assembly yourself is the only real approach that is going to work without restructuring your app. What it gives you is that the location of each and every assembly that the CLR fails to resolve can be determined and loaded by your code at runtime... I've used this in similar situations for both pluggable architectures and for an assembly reference integrity scanning tool.

You can use the <probing> element in a manifest file to tell the Runtime to look in different directories for its assembly files.

http://msdn.microsoft.com/en-us/library/823z9h8w.aspx

eg:

<configuration>
 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <probing privatePath="bin;bin2\subbin;bin3"/>
  </assemblyBinding>
 </runtime>
</configuration>

You can use the <codeBase> element found in the application configuration file. More information on " Locating the Assembly through Codebases or Probing ".

Well, the loaded assembly doesn't have an application configuration file.

Well if you know the specific folders at runtime you can use Assembly.LoadFrom .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM