4
votes

I want to know how to load assemblies which are outside the Appbase and Appdomain.

My problem is that I have a list of assemblies that are on a shared directory. My application needs to load these assemblies, which are located outside the path specified in the Appbase (path to the executable). I do not want to move them into the Appbase folder.

For more information, I have an application which is running in a distributed domain to test a collection of assemblies. When the application starts, it loads these assemblies from an array. When I test this application on my local desktop, it works well (loads and make reflection from assemblies, etc.), but from the cluster computers, it can't load those same assemblies, and throws the following Exception:

FileNotFoundException. Could not load file or assembly or one of its dependencies. The system cannot find the file specified.

4
It might help if you provided some information about why you want to load these assemblies. Do they contain plugins, or are they referenced at compile time?Justin
For more information,i have an application who running in a distributed domain and the goal of that application: it's to test a collection of assemblies tests.When the application starts,it loads assemblies from an array of assemblies path who aren't in the same location of the application and when i test this application on my local desktop :it's works well(loads and make reflection from assemblies etc..) but from the cluster computers,it can't load assemblies(FileNotFoundException.Could not load file or assembly or one of its dependencies. The system cannot find the file specified).Bordel Bordel

4 Answers

3
votes

There is an event that is fired if it can't find an assembly or one of its references:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {

        }

Add it before you're loading your assemblies. Put a breakpoint in it and in args there should be some info about the assembly you're missing

3
votes

What About MSDN - Assembly Load File

string filePath = "C:\asmPath";
Assembly myAssembly = Assembly.LoadFile(filePath);

You can also define a probe path in your app.config (which I consider a better solution) and having the CLR loading the assemblies on demand. MSDN Probing Path

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

7 1/2 years late. You can't https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/specify-assembly-location

The directories specified in privatePath must be subdirectories of the application base directory.

1
votes

thanks to @hcb's instruction I finally solve this problem:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name.Contains("Dll2Find"))
    {
        try
        {
            return Assembly.LoadFrom(@"D:\findHere\Dll2Find.dll");
        }
        catch (Exception)
        {
            return null;
        }
    }
    return null;
}