Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Reference dll from other location

Tags:

c#

.net

dll

I'm making a program depending on some DLLs included in a third party program. I'm not allowed to distribute these DLLs myself. The third party program must be installed for my program to work.

How can i make a reference to these DLLs? I know the exact location of them through a registry key set by the program.

I have tried to add the files in Project->References and set CopyLocal to false but when i start i then get a FileNotFoundException "Could not load file or assembly".

I have tried to add an event to AppDomain.CurrentDomain.AssemblyResolve and load the files there but the problem is that i get the exception before my program even starts. Even if i put a breakpoint on the first line the exception will be thrown before the breakpoint is hit.

like image 459
aero Avatar asked Aug 07 '11 10:08

aero


People also ask

How do I reference a DLL in C #?

Copy the downloaded DLL file in a custom folder on your dev drive, then add the reference to your project using the Browse button in the Add Reference dialog. Be sure that the new reference has the Copy Local = True .

How do I add a reference to System Web DLL?

1 right click on References in Solution Explorer and press add reference... 2 choose the browse tab and go to C:\Windows\assembly\GAC_32\System. Web\System.


1 Answers

From C# 3.0 in a Nutshell, 3rd edition, by Joseph and Ben Albahari, p. 557-558:

Deploying Assemblies Outside the Base Folder

Sometimes you might choose to deploy assemblies to locations other than the application base directory [...] To make this work, you must assist the CLR in finding the assemblies outside the base folder. The easiest solution is to handle the AssemblyResolve event.

(We can ignore the fact that in your case, someone other than you is deploying the assemblies.)

Which you tried. But a very important clue follows somewhat later. Read the two code comments:

public static void Loader
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += FindAssem;

        // We must switch to another class before attempting to use
        // any of the types in C:\ExtraAssemblies:
        Program.Go();
    }

    static Assembly FindAssem(object sender, ResolveEventArgs args)
    {
        string simpleName = new AssemblyName(args.Name).Name;
        string path = @"C:\ExtraAssemblies\" + simpleName + ".dll";

        if (!File.Exists(path)) return null;
        return Assembly.LoadFrom(path);
    }
}

public class Program
{
    public static void Go()
    {
        // Now we can reference types defined in C:\ExtraAssemblies
    }
}

As you see, the class where you resolve the external assemblies must not refer to any type from any of the external DLLs anywhere. If it did, code execution would stop way before your AssemblyResolve ever gets a chance to run.

like image 131
stakx - no longer contributing Avatar answered Oct 05 '22 12:10

stakx - no longer contributing