locked
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:
    [Export(typeof(AssetInterface))]
    [ExportMetadata("Name", "Something")]
    [ExportMetadata("TargetApplication", "MyApplication")]
    [ExportMetadata("Version", "0.1")]
    class PluginInterface : AssetInterface
    {
     //Functions
    }

    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.
    [ImportMany]
    IEnumerable<Lazy<AssetInterface, AssetMetaData>> plugins { get; set; }
    
    public void Compose()
    {
    	try
    	{
    		//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
    		batch.AddPart(this);
    		//Adds or removes parts in the batch from the container and executes the composition
    		container.Compose(batch);
    	}
    	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.

    Thanks

    Friday, November 30, 2012 10:33 AM