Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: MEF exports failure in MVC app

$
0
0

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; }
}
b) a sample plugin exporting a controller like:

[ExportController("Alpha")]
[PartCreationPolicy(CreationPolicy.NonShared)]publicsealedclass 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?


Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>