Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello,

i'm trying to implement a software by using the Managed Extensibility Framework (MEF). It's running but if there's a component in the directory, which exports an interface wich the container should import, but the implementation is not valid and implements only one method not in the right way, the ComposeParts()-method throws an exception and after this i got nothing in the container.
What i would expect was, that all correctly implemented components are in the container and only the invalid one is rejected.

Did i miss something, what i should configure or set by any method oder anything else in the Managed Extensibility Framework, so it just rejects the invalid components but keeps or gets all correct components.

here's the exception:
(in german ... sorry^^)
loader exception: Die Methode ""ProductionStart"" im Typ ""roboVision.Acquisition.TestImpl_1_IAcquisition"" der Assembly ""Acquisition_TI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" hat keine Implementierung.

(translation: the method ""ProductionStart"" in the type ..... has no implementation.)

after this, there is nothing in the container. But were are many other components of other export types. if i delete the invalid one from the folder, all other are added to the container correctly.

I hope, i could write this understandable and you could help me.
Thanks in advance
Vin
Posted
Updated 2-Apr-17 20:47pm

1 solution

Ok ... i solved it! \o/

if any other guy has a similar problem, here's my code:

C#
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Kernel).Assembly));
//catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));

foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory))
{
    if (file.EndsWith(".dll"))
    {
        var tmpCatalog = new AggregateCatalog();
        try
        {
            tmpCatalog.Catalogs.Add(new AssemblyCatalog(file));
            CompositionContainer tmpContainer = new CompositionContainer(tmpCatalog);
            tmpContainer.ComposeParts(this);
            tmpContainer.Dispose();
            //this will only executed, if the former trail to compose doesn't cause an exception
            // after this, the catalog should only contain assemblies, which could be composed later
            catalog.Catalogs.Add(new AssemblyCatalog(file));
        }
        catch (ReflectionTypeLoadException rtlEx)
        {
            logger.Log(LogLevel.Error, "ReflectionTypeLoadException while trying to add " + file.ToString() + " : " + rtlEx.Message + "\r\n" + rtlEx.StackTrace);
            //System.Windows.Forms.MessageBox.Show("ReflectionTypeLoadException while trying to add " + file.ToString() + " : " + rtlEx.Message + "\r\n" + rtlEx.StackTrace);
            foreach (Exception _ex in rtlEx.LoaderExceptions)
            {
                logger.Log(LogLevel.Error, "   loader exception: " + _ex.Message + "\r\n" + _ex.StackTrace);
                //System.Windows.Forms.MessageBox.Show("   loader exception: " + _ex.Message + "\r\n" + _ex.StackTrace);
            }
        }
        catch (Exception ex)
        {
            logger.Log(LogLevel.Error, "Exception while trying to add " + file.ToString() + " : " + ex.Message + "\r\n" + ex.StackTrace);
            //System.Windows.Forms.MessageBox.Show("Exception while trying to add " + file.ToString() + " : " + ex.Message + "\r\n" + ex.StackTrace);
        }
    }
            
}

//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);



//Fill the imports of this object
try
{
                    
    this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
    logger.Log(LogLevel.Error, "error at composing parts: " + compositionException.Message + "\r\n" + compositionException.StackTrace);
    System.Windows.Forms.MessageBox.Show("error at composing parts: " + compositionException.Message + "\r\n" + compositionException.StackTrace);
                    
}
catch (CompositionContractMismatchException contractMissmatchEx)
{
    logger.Log(LogLevel.Error, "contract missmatch: " + contractMissmatchEx.Message + "\r\n" + contractMissmatchEx.StackTrace);
    System.Windows.Forms.MessageBox.Show("contract missmatch: " + contractMissmatchEx.Message + "\r\n" + contractMissmatchEx.StackTrace);
}
catch (ReflectionTypeLoadException typeLoadEx)
{
    logger.Log(LogLevel.Error, "type load exception: " + typeLoadEx.Message + "\r\n" + typeLoadEx.StackTrace);
    System.Windows.Forms.MessageBox.Show("type load exception: " + typeLoadEx.Message + "\r\n" + typeLoadEx.StackTrace);
    foreach (Exception _ex in typeLoadEx.LoaderExceptions)
    {
        logger.Log(LogLevel.Error, "   loader exception: " + _ex.Message + "\r\n" + _ex.StackTrace);
        System.Windows.Forms.MessageBox.Show("   loader exception: " + _ex.Message + "\r\n" + _ex.StackTrace);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900