Search code examples
c#castle-windsormef

How to fill IEnumerable-Property of Object using MEF and DirectoryCatalog


What I am basically trying to achieve is to combine MEF and Castle Windsor. But I happen to be unable to get started using MEF. The Problem is, when I call ComposeParts on the compositionContainer in the MefInstaller-Class, it doesn't fill the installers-collection, for some reason.

The DirectoryCoatalog words fine and loads the needed files properly (including ModuleA.dll).

public class MefInstaller : IWindsorInstaller
{
    [ImportMany]
    public IEnumerable<IWindsorInstaller> installers { get; set; }  // here the Exported Objects should be stored

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        var directory = Environment.CurrentDirectory;

        var directoryCatalog = new DirectoryCatalog(directory); 
        var compositionContainer = new CompositionContainer(directoryCatalog);

        compositionContainer.ComposeParts(this);

        foreach (var windsorInstaller in installers)
        {
            windsorInstaller.Install(container, store);
        }

        Console.WriteLine("List in Field : {0}", installers.Count());
    }
}

The Class to import looks as the following:

[Export("ComponentInstaller")]
public class ModuleAInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container
            .Register(Component.For<IFoo>().ImplementedBy<FooA>())
            .Register(Component.For<IBar>().ImplementedBy<BarA>());
    }
}

What am i doing wrong? I have experimented with different attributes and contract names on both the MefInstaller and ModuleAInstaller-Classes. I have also tried to compose the parts using a CompositionBatch with no success. Any help is greatly appreciated!


Solution

  • Nevermind, I have solved it. The problem was the attribute on the ModuleAInstaller-Class.

    everything works fine using

    [Export(typeof(IWindsorInstaller))]
    public class ModuleAInstaller : IWindsorInstaller
    {
        [...]
    }