MEF loading multiple plugins built from the same project RRS feed

  • Question

  • I have a number of plugins each one containing a resource dictionary and a simple MEF implementation for getting the DLL filename and the resource dictionary URI and basic export MetaData for: Theme Name, Target Application and Version. All these DLLs are put into a theme folder and are loaded at runtime to allow a user to select a theme for an application.

    Heres the problem: If I have two DLLs with different tags in the MetaData for Version and I save them into the same directory (with different filenames of course), when I load them in via my MEF loader both copies appear to be loaded but both share the same MetaData. I would expect to see both versions and be able to use the version MetaData tag to distinguish between them, but this is not the case.

    Here is the export data I am providing to the asset projects:
    [ExportMetadata("Name", "Something")]
    [ExportMetadata("TargetApplication", "MyApplication")]
    [ExportMetadata("Version", "0.1")]
    class PluginInterface : AssetInterface

    Here is the plugin loading code I am using. I use the MetaData later on to determine the latest version of the plugin but since the MetaData is appearing the same for different instances its useless till this problem is addressed.
    IEnumerable<Lazy<AssetInterface, AssetMetaData>> plugins { get; set; }
    public void Compose()
    		//Discovers attributed parts int the assembilies of the specified directory
    		DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory + "\\themes");
    		//Catalog that combines the elements of ComposablePartCatalog objects
    		AggregateCatalog agCatalogue = new AggregateCatalog(new ComposablePartCatalog[] { catalog, new AssemblyCatalog(Assembly.GetExecutingAssembly()) });
    		//Manages the parts of the composition
    		CompositionContainer container = new CompositionContainer(agCatalogue);
    		//Represents a set of ComposablePart objects which can be added/removed from a composition
    		CompositionBatch batch = new CompositionBatch();
    		//Creates a ComposablePart from the attributed object
    		//Adds or removes parts in the batch from the container and executes the composition
    	catch (FileNotFoundException fex)
    		Console.WriteLine("File not found exception : " + fex.Message);
    	catch (CompositionException cex)
    		Console.WriteLine("Composition exception : " + cex.Message);
    	catch (DirectoryNotFoundException dex)
    		Console.WriteLine("Directory not found exception : " + dex.Message);

    My question is why is MEF combining both versions of the DLL and presenting me with multiple copies of the same thing. Is it a namespace issue, are there specfic export tags I am missing that allow it to load or is it to do with using lazy loading? How do I get multiple DLLs with different version MetaData to load seperately.


    Friday, November 30, 2012 10:33 AM