First of all: you are all healthy, I very often look for this site for MEF answers.
My problem is this:
I have several assemblies with many [Import] in them and one main application where the assembly takes place. Now the problem is that these Imports are not filled, they always remain zero.
I tried to reproduce this behavior in a simple small project and came up with the following source code.
Did I miss some things about MEF?
Please, help! Thanks everyone!
Build Interfaces:
namespace Interfaces { public interface IClass1 { void Trigger(); } public interface IClass2 { void Trigger(); } public interface IClass3 { void Trigger(); } }
Assembly Library1:
namespace Library1 { [Export(typeof(IClass1))] public class Class1:IClass1 { #region IClass1 Members public void Trigger() { } #endregion } }
Assembly Library2:
namespace Library2 { [Export(typeof(IClass2))] public class Class2:IClass2 { [Import] public IClass1 Class1 { get; set; } public void Trigger() { } } }
In the main program, I collect all the Mef stuff by doing the following:
namespace MEFTest { public class mefStart { public CompositionContainer Container { get; private set; } public void Start() { AggregateCatalog catalog = new AggregateCatalog(); AssemblyCatalog assemblyCatalog = new AssemblyCatalog(typeof(Program).Assembly); DirectoryCatalog directoryCatalog = new DirectoryCatalog(".", "Library*.dll"); catalog.Catalogs.Add(directoryCatalog); catalog.Catalogs.Add(assemblyCatalog); this.Container = new CompositionContainer(catalog); CompositionBatch batch = new CompositionBatch(); batch.AddExportedValue(this.Container); this.Container.Compose(batch); this.Container.ComposeParts(this); } } }
But after that, the following class has no populated imports:
namespace MEFTest { public class Class3:IClass3 { [Import] public IClass1 Class1 { get; set; } [Import] public IClass2 Class2 { get; set; } public void Trigger() { Class1.Trigger(); Class2.Trigger(); } } }
When I look at the container, I see that IClass1 and ICLass2 have been compiled.
Why is [Import] not performed in Class3? I guess I'm missing something ...
Thank you all for your help!
Michael