MEF组合错误:仅在某些机器上

时间:2012-06-26 16:18:02

标签: .net components mef composition

我有一个使用MEF的控制台应用程序。它在本地和部署到开发机器时工作正常。当部署到发布机器时,它会引发组合错误。

开发和发布机器都是虚拟的sql server 2008 r2机器,具有相同的规范,并且大多数安装了相同的软件和组件。有一个构建和部署过程,但即使从dev复制文件以释放和运行它们也会导致相同的错误。

在发布计算机或权限上可能缺少MEF所需的任何先决条件吗?

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Resulting in: An exception occurred while trying to create an instance of type 'XXX.XXX.Dispatch.EmailDispatcher'.

Resulting in: Cannot activate part 'XXX.XXX.Dispatch.EmailDispatcher'.
Element: Lifetime.CrmBroker.Dispatch.EmailDispatcher -->  XXX.XXX.Dispatch.EmailDispatcher -->  AssemblyCatalog (Assembly="XXX.XXX, Version=1.1.2264.871, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot get export 'XXX.XXX.Dispatch.EmailDispatcher (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher")' from part 'XXX.XXX.Dispatch.EmailDispatcher'.
Element: XXXX.XXX.Dispatch.EmailDispatcher (ContractName="XXXX.XXX.Dispatch.IEntityTypeDispatcher") -->  XXX.XXX.Dispatch.EmailDispatcher -->  AssemblyCatalog (Assembly="XXX.XXX, Version=1.1.2264.871, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot set import 'XXX.XXXX.Dispatch.DispatcherRepository.Dispatchers (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher")' on part 'XXX.XXX.Dispatch.DispatcherRepository'.
Element: XXX.XXX.Dispatch.DispatcherRepository.Dispatchers (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher") -->  XXX.XXX.Dispatch.DispatcherRepository
 (System.ComponentModel.Composition.CompositionException)

3 个答案:

答案 0 :(得分:1)

添加一些代码以捕获CompositionException并获取有关的信息 根本原因是这样的:

catch (CompositionException e)
{
    // "unable to load one or more of the requested types" hints at a
    // ReflectionTypeLoadException, so cast to that
    var loadException = (ReflectionTypeLoadException)e.Errors.First(); 

    // as the error said,
    // "Retrieve the LoaderExceptions property for more information"
    var cause = loadException.LoaderExceptions.First();

    // print, log or extract the information in some other way 
    Debug.Print(cause.Message);
}

虽然它似乎只是一个缺失的依赖。

答案 1 :(得分:1)

感谢回复人员,有关调试MEF的方便提示。

事实证明问题是我没有在机器上安装Entity Framework 4.1。 MEF正在吞噬本来会抛出的异常,我从来没有找到它,但我在环境之间进行了详细的比较,并尝试安装EF 4.1以确定它是否有效。

答案 2 :(得分:0)

检查动态加载类型的构造函数。 MEF隐藏了MEF错误背后的构造函数中的问题。确保在构造函数中有错误处理,然后添加一些日志记录以捕获“真正的”异常。