【问题标题】:MEF Composition Errors: Only on some machinesMEF 组合错误:仅在某些机器上
【发布时间】:2012-06-28 00:36:12
【问题描述】:

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

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

我是否需要 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)

【问题讨论】:

    标签: .net components mef composition


    【解决方案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);
    }
    

    这似乎只是一个缺失的依赖项。

    【讨论】:

      【解决方案2】:

      感谢大家的回复,以及调试 MEF 的实用技巧。

      原来问题是我的机器上没有安装 Entity Framework 4.1。 MEF 正在吞噬本应抛出的异常,我从未找到它,但我对环境进行了详细比较,然后尝试安装 EF 4.1 以查看它是否有效。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-27
          • 2015-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-11
          • 1970-01-01
          相关资源
          最近更新 更多