如何调试MEF异常? [英] how to debug MEF exception?

查看:144
本文介绍了如何调试MEF异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在使用MEF(托管扩展框架, http://mef.codeplex.com/ ),它会抛出异常,并且继续进行的信息有限.

we are currently using MEF (Managed Extensibility Framework, http://mef.codeplex.com/ ) and it throws out exceptions, with limited information to proceed on.

有没有一种方法可以调试MEF异常?

is there a way to debug MEF exceptions?

我的例外情况是这样的:

My exception is like this:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)

   at System.Reflection.RuntimeModule.GetTypes()

   at System.Reflection.Assembly.GetTypes()

   at System.ComponentModel.Composition.Hosting.AssemblyCatalog.get_InnerCatalog()

   at System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetExports(ImportDefinition definition)

   at System.ComponentModel.Composition.Hosting.AggregateCatalog.GetExports(ImportDefinition definition)

   at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)

   at System.ComponentModel.Composition.Hosting.AggregateExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)

   at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExports(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)

   at System.ComponentModel.Composition.Hosting.CompositionContainer.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)

   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ImportEngine.TryGetExports(ExportProvider provider, ComposablePart part, ImportDefinition definition, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportSubset(PartManager partManager, IEnumerable`1 imports, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ImportEngine.TryPreviewImportsStateMachine(PartManager partManager, ComposablePart part, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ImportEngine.PreviewImports(ComposablePart part, AtomicComposition atomicComposition)

   at System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch batch)

   at System.ComponentModel.Composition.Hosting.CompositionContainer.Compose(CompositionBatch batch)

   at System.ComponentModel.Composition.AttributedModelServices.ComposeParts(CompositionContainer container, Object[] attributedParts)

   at MyApp.Extension..ctor(Assembly assembly) in W:\MyApp\Source\\Extensions\Extension.cs:line 45

代码很简单:

var aggregateCatalog = new AggregateCatalog();
_assembly = assembly;
var assemblyCatalog = new AssemblyCatalog(assembly);
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
aggregateCatalog.Catalogs.Add(assemblyCatalog);
_compositionContainer = new CompositionContainer(aggregateCatalog);
_compositionContainer.ComposeParts(this);

推荐答案

诊断组成问题在MEF编程指南中,或在MSDN上的调试MEF 主题中.

I feel your pain. When I'm stuck, I typically dump the MEF composition information to get more information on the cause of the composition failure. For instructions on how to do this, see Diagnosing Composition Problems in the MEF Programming Guide or the Debugging MEF topic on MSDN.

.NET 4.5(或Codeplex上的当前MEF 2预览版4版本)中有一个更简单的选项:您可以通过在

In .NET 4.5 (or the current MEF 2 preview 4 release available on codeplex) there is a simpler option: you can improve the usefulness of the error message by disabling silent rejection in the CompositionOptions which you pass to the container constructor.

编辑:啊,您得到一个ReflectionTypeLoadException.那是另一回事:这意味着某些程序集中的类型无法成功加载,通常是因为它们引用了其他找不到的类型.在您的代码示例中,您应该能够通过调用assembly.GetTypes()来重现该问题,而无需涉及MEF.

edit: ah, you're getting a ReflectionTypeLoadException. That's another matter: it means that the types in some assembly cannot be successfully loaded, typically because they reference other types that cannot be found. In your code example, you should be able to reproduce the problem by invoking assembly.GetTypes(), without involving MEF.

这篇关于如何调试MEF异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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