ImportMany parameter in Constructor always null

Feb 10, 2011 at 3:50 PM

I have a controller that is exported using MEF and loaded by the Controller factory.

    
[Export(Controller)]
    public class MyController : Controller
    {

        private IRepository MyRepsoitory;

        [ImportMany]
        public IEnumerable<MyImportedItem> TestImportItems {get;set;}

        public MyController([ImportMany]IEnumberable<MyImportedItem> items, [Import]IRepository repository)
        {
            // items here is always null
            // However if I grab the container that the ControllerFactory used and tell it ComposeParts on this the TestImportItems will be filled with 50+ items
            // repository however is instantiated appropriately. 

            GlobalItems.Container.ComposeParts(this);
            //Now TestImportItems if filled but my items parameter alway null... how do I get constructor to fill

        }

    }

So MEF creates MyController but only creates the repository and sends null for the ImportMany even though it can fill the property later with the same Container.

What's also odd is if I do something that breaks one of the items the creation of MyConroller breaks in ControllerFactory.. as if it checks that is has parts for the constructor but never pushes them to the IEnumerable parameter.

What am I missing?

Obviously I have the parts available if the same Container works for .ComposingParts on (this) (and I reflected the catalog which has appropriate import/export Parts available at time of creating the Controller.

I could rewrite my class to use the filled Property but I would really like my importing constructor to get a filled collection.