Context problem while loading Assemblies via Structuremap

Posted by Zebi on Stack Overflow See other posts from Stack Overflow or by Zebi
Published on 2010-06-16T16:59:46Z Indexed on 2010/06/16 17:02 UTC
Read the original article Hit count: 208

Filed under:
|
|
|
|

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 he 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 they 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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about assembly