Hello, I'm testing with MEF in MVC and I'm experiencing an issue when getting exported parts from my catalog; probably it's just me, but I'm stuck with it. Here is the minimalist scenario:
a) a metadata attribute for exporting controllers from plugins. As such, this attribute will be shared among all the plugins and thus resides in its own independent assembly:
[MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]publicclass ExportControllerAttribute : ExportAttribute, IControllerMetadata {public ExportControllerAttribute(string name) : base(typeof(IController)) { Name = name; }publicstring Name { get; privateset; } }publicinterface IControllerMetadata {string Name { get; } }
[ExportController("Alpha")] [PartCreationPolicy(CreationPolicy.NonShared)]publicsealedclass AlphaController : Controller ...
AggregateCatalog catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()),new DirectoryCatalog(Path.Combine( Environment.CurrentDirectory, "Plugins/"))); CompositionContainer container = new CompositionContainer(catalog);var partDef = (from l in container.GetExports<IController, IControllerMetadata>()where l.Metadata.Name == "Alpha"select l).FirstOrDefault();if (partDef != null) IController controller = partDef.Value;