views:

28

answers:

1

I am trying to develop a plugin architecture in .Net. The application will be a .Net application. There will be directories which holds the plug-ins. Each directory will represent a plugin. Each plugin directory will also contain all the dependency dlls as well. The plugins need to be stored in separate AppDomain as the plugins may use the same assemblies, but different versions.

As it iterates through the foreach loop in Init(), I get a System.IO.FileNotFoundException : Could not load file or assembly '[Assembly Name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. in _apDomain.Load() for assemblies that are not in the main project.

Code:

    readonly private AppDomain _appDomain;
    internal ItemModuleAppDomain()
    {
        AppDomainSetup info = AppDomain.CurrentDomain.SetupInformation;
        _appDomain = AppDomain.CreateDomain("ChildDomain");
    }

    public void Init(string dllDir)
    {
        string[] dlls = Directory.GetFiles(dllDir, "*.dll", SearchOption.TopDirectoryOnly);
        foreach(string dll in dlls)
        {
            _appDomain.Load(AssemblyName.GetAssemblyName(dll));
        }

Any idea why? I tried several methods such as reading the assembly as an array of bytes to load.

A: 

I believe AppDomain.Load may be a wrong method for your purpose - it loads the assembly into both current app domain as well as target domain. And you get an error because your assembly is located in a sub folder and .NET does not know about it. You have to give private paths in configuration (http://msdn.microsoft.com/en-us/library/15hyw9x3.aspx). You can also use AssemblyResolve or TypeResolve event of AppDomain for resolution.

You should also look at AppDomain.CreateInstanceFrom method to load your main plugin type (and containing assembly).

VinayC
Thanks. We ended up strongly naming the dlls so we can load them into the same AppDomain.
Anish