如果任何找到的组件没有正确实现其接口,则MEF不会导入任何内容 [英] MEF imports nothing, if any found component does implement its interface not correctly

查看:112
本文介绍了如果任何找到的组件没有正确实现其接口,则MEF不会导入任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试使用Managed Extensibility Framework(MEF)来实现软件。它正在运行,但是如果目录中有一个组件,它导出一个容器应该导入的接口,但是实现无效并且只实现了一种方法不正确,ComposeParts() - 方法抛出异常并在此之后我在容器里什么都没有。

我所期望的是,所有正确实现的组件都在容器中,只有无效的组件被拒绝。



我错过了什么,我应该通过Managed Extensibility Framework中的任何其他方法配置或设置什么,所以它只是拒绝无效的组件,但保留或获取所有正确的组件。



这里有例外:

(德语...抱歉^^)

加载器异常:Die MethodeProductionStartim TyproboVision.Acquisition.TestImpl_1_IAcquisition der AssemblyAcquisition_TI,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = nullhat keine Implementierung。



(翻译:方法类型.....中的ProductionStart没有实现。)



之后,容器中没有任何内容。但是其他出口类型的许多其他组成部分。如果我从文件夹中删除了无效的一个,所有其他的都正确地添加到容器中。



我希望,我可以写这个可以理解,你可以帮助我。

提前致谢

Vin

解决方案

好的......我解决了! \ $ /



如果有其他人有类似的问题,这是我的代码:



< pre lang =c#> // 组合多个目录的聚合目录
var catalog = new AggregateCatalog();
// 添加与Program类相同的程序集中找到的所有部件
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();
尝试
{
tmpCatalog.Catalogs.Add( new AssemblyCatalog (文件));
CompositionContainer tmpContainer = new CompositionContainer(tmpCatalog);
tmpContainer.ComposeParts( this );
tmpContainer.Dispose();
// 这只会执行,如果要编写的前一个跟踪不会导致异常
// 此后,目录应该只包含可以在以后编写的程序集
catalog.Catalogs.Add( new AssemblyCatalog(file));
}
catch (ReflectionTypeLoadException rtlEx)
{
logger.Log(LogLevel.Error, ReflectionTypeLoadException尝试添加 + file.ToString()+ + rtlEx.Message + \ r \ n + rtlEx.StackTrace);
// System.Windows.Forms.MessageBox.Show(ReflectionTypeLoadException尝试添加+文件。 ToString()+:+ rtlEx.Message +\\\\ n+ rtlEx.StackTrace);
foreach ( rtlEx.LoaderExceptions中的异常_ex
{
logger.Log(LogLevel.Error, 加载程序异常: + _ex.Message + \\\\ n + _ex.StackTrace);
// System.Windows.Forms.MessageBox.Show(loader exception:+ _ex.Message + \\\\ n+ _ex.StackTrace);
}
}
catch (Exception ex )
{
logger.Log(LogLevel.Error, 尝试添加时出现异常 + file.ToString()+ + ex.Message + \\\\ n + ex.StackTrace);
// System.Windows.Forms.MessageBox.Show(尝试添加时出现异常+文件。 ToString()+:+ ex.Message +\\\\ n+ ex.StackTrace);
}
}

}

// 使用目录中的部件创建CompositionContainer
_container = new CompositionContainer(catalog);



// 填写此对象的导入
尝试
{

。 _container.ComposeParts( this );
}
catch (CompositionException compositionException)
{
logger.Log(LogLevel.Error, 组成部分时出错: + compositionException.Message + \\\\ n + compositionException.StackTrace);
System.Windows.Forms.MessageBox.Show( 撰写零件时出错: + compositionException.Message + \\\\ n + compositionException.StackTrace);

}
catch (CompositionContractMismatchException contractMissmatchEx)
{
logger.Log(LogLevel.Error,< span class =code-string>
合同失配: + contractMissmatchEx.Message + \\\\ n + contractMissmatchEx.StackTrace);
System.Windows.Forms.MessageBox.Show( 合约失配: + contractMissmatchEx。消息+ \\\\ n + contractMissmatchEx.StackTrace);
}
catch (ReflectionTypeLoadException typeLoadEx)
{
logger.Log(LogLevel.Error, 类型加载异常: + typeLoadEx.Message + < span class =code-string> \\\\ n + typeLoadEx.StackTrace);
System.Windows.Forms.MessageBox.Show( 类型加载异常: + typeLoadEx .Message + \\\\ n + typeLoadEx.StackTrace);
foreach (Exception _ex in typeLoadEx.LoaderExceptions)
{
logger.Log(LogLevel.Error, loader exception: + _ex.Message + \\\\ n + _ex.StackTrace);
System.Windows.Forms.MessageBox.Show( loader exception: + _ex。消息+ \\\\ n + _ex.StackTrace);
}
}


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

解决方案

Ok ... i solved it! \o/

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

//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);
    }
}


这篇关于如果任何找到的组件没有正确实现其接口,则MEF不会导入任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆