Question

My application uses a third party SDK which is installed on every machine that has my application, I do not want to add these SDK DLLs in my application deployment because it takes a lot of space. Registering the DLLs in the GAC is not an option for other reasons. Anyway is there a way to tell my application that the required DLLs are available on a specific path?

Was it helpful?

Solution 2

Yes, shared files (if for some reason you do not want to put them in GAC) can be deployed, for example, in the Common Programs folder.

According to the way your application works you may need to load them explictly with Assembly.LoadFrom() using the path you get from Environment.GetSpecialFolder() for Environment.SpecialFolders.CommonPrograms or attaching and event handler for AppDomain.AssemblyResolve event.

Let's see a raw and simple example:

// Put this in your initialization code
public static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveAssembly(MyResolveEventHandler);
}

private static Assembly ResolveAssembly(object sender, ResolveEventArgs e)
{
    // Clean, check, double check and verify path name to be sure it's a valid
    // assembly name and it's not a relative path itself, you may even check e.RequestingAssembly
    string path = ...; 

    return Assembly.LoadFrom(path);
}

If you have the directory and you want to load them all at startup (just in case...):

private static LoadThemAll(folderPath)
{
    foreach (var path in Directory.GetFiles(folderPath, "*.dll"))
        Assembly.LoadFrom(path);
}

Do not forget to add proper error handling (for non managed DLLs, wrong versions and everything else may happen, especially because that assemblies will be loaded in your AppDomain and everyone may put a malicious one in that folder).

Of course all of these can be applied to any (accessible) folder, which one is the best...well it depends on what you're trying to load and how it's deployed (you can even search in Registry to dynamically find where support files are located).

Finally in case assemblies you referenced are strong signed and they reside on a known, fixed, immutable location you can use <codebase> in your .config file.

OTHER TIPS

We have used the probing path in asp.net applications to do something similar in the past:

http://msdn.microsoft.com/en-us/library/823z9h8w.aspx

Has a side affect in asp.net where the assemblies in these locations will not be loaded automatically when the application runs, only when they cannot be found in bin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top