tags:

views:

1086

answers:

5

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?

Thanks, Jens

+1  A: 

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

e.g.:

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

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.

Shaun Austin
A: 

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.

John
A: 

Well, the loaded assembly doesn't have an application configuration file. I cannot use a config file because the assemblies to be loaded are determined at runtime. :(

Edit: I'll try to restate. (Thanks for the help so far!)

The application dynamically loads assemblies from well-known sub-directories. These assemblies have static references to other assemblies. I want to deploy those dependencies with its dependent assembly together into one directory - a sub directory of the main application.

The goal is to have a folder structure like this:

  • Modules
    • Module1
      • Module1.dll
      • 3rdParty.dll
      • Theme1.dll
    • Module2
      • Module2.dll
      • OtherComp.dll
      • Theme2.dll
  • Shell.exe

So Shell.exe knows loads "Module1.dll" and "Module2.dll" with LoadFrom() dynamically. "3rdParty.dll" and "Theme1.dll" are dependencies of "Module1.dll" and linked statically. But the runtime is looking for them in the application's base directory instead of the Module1's sub directory.

FantaMango77
A: 

I have a related question on whether this approach will also allow me to dynamically choose between 32 & 64bit components at runtime. Click here for link.

If this answer does solve the issue, that's double the points!

Greg Whitfield