Question

This is a really long one, so hang tight.

The idea is that an application, when referenced to Core.dll (see below), will create a new object of type Core, and in doing so, the constructor will scan the C:\Problems folder for any DLLs which contain a type that uses the IPlugin interface.

It works when I reference to Core.dll from a console application. It does not work when I do the equivalent from PowerShell, unless Core.dll is in the GAC.

These are the details:

I have an assembly (Core.dll) which is the result of:

public class Core
{
    List<IPlugin> plugins = new List<IPlugin>();

    public Core(string path)
    {
        LoadPlugins(path);
        foreach (IPlugin plugin in plugins)
            plugin.Work();
    }

    void LoadPlugins(string path)
    {
        foreach (string file in Directory.GetFiles(path))
        {
            FileInfo fileInfo = new FileInfo(file);
            Assembly dllAssembly = Assembly.LoadFile(file);
            foreach (Type type in dllAssembly.GetTypes())
            {
                Type typeInterface = type.GetInterface("Problems.IPlugin");
                if (typeInterface != null)
                    plugins.Add((IPlugin)Activator.CreateInstance(
                                    dllAssembly.GetType(type.ToString())));
            }
        }
    }
}

public interface IPlugin
{
    string Name {get; set;}
    void Work();
}

Then, I have a separate assembly, MyPlugin.dll, that contains the following:

public class MyPlugin : IPlugin
{
    public string Name { get; set; }
    public void Work()
    {
        Console.WriteLine("Hello, World. I'm a plugin!");
    }
}

I tested this all with a third project, TestApp.exe (with Core.dll as a reference):

static void Main(string[] args)
{
    Core core = new Core(@"C:\Problems");
    Console.ReadLine();
}

I put both Core.dll and MyPlugin.dll into C:\Problems, and TestApp.exe works like a charm! So, I write this PowerShell script:

Add-Type -Path "C:\Problems\Core.dll"
$core = New-Object Problems.Core "C:\Problems"

This is my result (imagine a bunch of red):

New-Object : Exception calling ".ctor" with "1" argument(s): "Unable to load one or more o
f the requested types. Retrieve the LoaderExceptions property for more information."
At line:1 char:11
+ New-Object <<<<  Problems.Core "C:\Problems"
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Drilling down into the Exception, I find this:

PS C:\Problems> $Error[0].Exception.InnerException.LoaderExceptions
Could not load file or assembly 'Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

My conclusion so far is that when I'm running GetTypes() as part of the plugin loader, it's seeing that MyPlugin inherits from Problems.IPlugin, which it doesn't know where to find. But, didn't I already load it in Core.dll?

The only way I can get around this is to sign the Core.dll assembly and add it to the GAC, but that is both annoying and does not fit my purposes.

(All classes are in the Problems namespace, and I omitted a ton of cleanup and filtering logic to illustrate the problem.)

Was it helpful?

Solution

You can solve this by making sure you handle assembly resolve issues by looking in the right place, i.e. change your Core class to be something like this:

public class Core
{
    List<IPlugin> plugins = new List<IPlugin>();
    string path;

public Core(string path)
{
    this.path = path;
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    LoadPlugins();
    foreach (IPlugin plugin in plugins)
        plugin.Work();
}

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();

    foreach (var assembly in currentAssemblies)
    {
        if (assembly.FullName == args.Name)
        {
            Console.WriteLine("resolved assembly " + args.Name + " using exising appdomain");
            return assembly;
        }
    }

    var file = args.Name.Split(new char[] { ',' })[0].Replace(@"\\", @"\");
    var dll = Path.Combine(path, file) + ".dll";
    Console.WriteLine("resolved assembly " + args.Name + " from " + path);
    return File.Exists(dll) ? Assembly.LoadFrom(dll) : null;
}

void LoadPlugins()
{
    foreach (string file in Directory.GetFiles(this.path, "*.dll"))
    {
        FileInfo fileInfo = new FileInfo(file);
        Assembly dllAssembly = Assembly.LoadFile(file);
        foreach (Type type in dllAssembly.GetTypes())
        {
            Type typeInterface = type.GetInterface("Problems.IPlugin");
            if (typeInterface != null)
                plugins.Add((IPlugin)Activator.CreateInstance(
                                dllAssembly.GetType(type.ToString())));
        }
    }
}

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