Loading assemblies and its dependencies
Asked Answered
C

3

22

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?

Clung answered 22/8, 2008 at 10:6 Comment(0)
J
19

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.

Jupiter answered 22/8, 2008 at 10:15 Comment(2)
A good example of this technique is the application LINQPad. It ships as a single exe, so all the libraries are included as embedded resources. See albahari.com/nutshell/ch16.aspx for code and linqpad.net/HowLINQPadWorks.aspx for insight.Paige
I've tried this method, and it StackOverFlow's on asking for the same assembly over and over...Pneumococcus
P
4

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>
Propertius answered 22/8, 2008 at 10:13 Comment(0)
T
1

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.

Trollop answered 22/8, 2008 at 10:19 Comment(1)
The first link gives "Topic no longer exists". Specifying .Net 4.0 brings it up though: https://msdn.microsoft.com/en-us/library/15hyw9x3%28v=vs.100%29.aspxSoul

© 2022 - 2024 — McMap. All rights reserved.