This project has moved and is read-only. For the latest updates, please go here.

MEF exports failure in MVC app

Jan 13, 2013 at 7:40 PM

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:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportControllerAttribute : ExportAttribute, IControllerMetadata
    public ExportControllerAttribute(string name) : base(typeof(IController))
        Name = name;
    public string Name { get; private set; }

public interface IControllerMetadata
    string Name { get; }

b) a sample plugin exporting a controller like:

public sealed class AlphaController : Controller

c) a consumer console app which gets the exported controller like:

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;

This works fine and the exported controller is located and instantiated. (I copied the plugin DLL in its Plugins folder). If instead I try exactly the same code in a MVC web app (Global.asax, copying the same DLLs in a Plugins folder), I can see that the catalog contains my part, but no export are found by the LINQ query above. Could anyone explain?

Jan 14, 2013 at 9:34 PM


  Hi- sounds strange. Have you checked that all code is being built against
the same MVC version, or that the required binding redirects are in place?