views:

21

answers:

1

I want to load plugins in a smiliar way as shown here however the loaded assemblies seem not to share the same context.

Trying to solve the problem I just build a tiny spike containing two assemblies. One console app and one library. The console app contains the IPlugin interface and has no references to the Plugin dll.

I am scanning the plugin dir using a custom Registration convention:

ObjectFactory.Initialize(x => x.Scan(s =>
    {
        s.AssembliesFromPath(@"..\Plugin");
        s.With(new PluginScanner());
    }));

public void Process(Type type, Registry registry)
{
    if (!type.Name.StartsWith("P")) return;

    var instance = ObjectFactory.GetInstance(type);
    registry.For<IPlugin>().Add((IPlugin)instance);
}

Which thows an invalid cast exception saying "can not convert the plugin Type to IPlugin".

public class P1 : IPlugin
{
    public void Start() { Console.WriteLine("Hello from P1"); }
}

Further if I just construct the instance (which works fine by the way) and try to access ObjectFactory in the plugin ObjectFactory.WhatDoIHave() shows that plugin instance and host instance don't even share the same container instance.

Experimenting around with MEF, Structuremap and loading the assembly manually whith Assembly.Load("Plugin") shows if loaded with Assembly.Load it works fine. Any ideas how I can fix this to work with StructureMaps assembly scanning?

A: 

I found the solution.

Take this structure:

\Plugins
        \Plugin.dll
        \core.dll
\app.exe
\core.dll

So IPlugin is defined in core.dll. My Plugin has a dependency to the core, as well as my app. My app loads the core.dll and loads the plugin.dll which will search for its dependency in its plugin folder. Loading core.dll a second time.

If I get a type from Plugin.dll and try to cast it to IPlugin in the core the app tries to cast an object which is subclass of {PluginDir}Core.IPlugin to {AppDir}Core.IPlugin. This will fail because the interfaces are different ones.

\Plugin.dll
\app.exe
\core.dll    

This scenario will work fine because both app.exe and plugin.dll wil use the same core.dll.

Zebi