Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading assemblies and its dependencies

Tags:

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?

like image 200
FantaMango77 Avatar asked Aug 22 '08 10:08

FantaMango77


People also ask

What is load assembly?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Load(Byte[]) Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.

Could not load the file or assembly or one of its dependencies?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

What is the method to load assembly given its file name and its path?

LoadFrom(String) Loads an assembly given its file name or path.

What is the role of assemblies in CLR?

Assemblies in the common language runtime. Assemblies provide the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.


2 Answers

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.

like image 91
Shaun Austin Avatar answered Sep 21 '22 09:09

Shaun Austin


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>
like image 21
samjudson Avatar answered Sep 20 '22 09:09

samjudson