0

我有一个使用 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)
4

3 回答 3

2

添加一些代码来捕获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);
}

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

于 2012-06-26T20:15:55.090 回答
1

感谢各位的回复,以及调试 MEF 的便捷技巧。

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

于 2012-06-27T14:43:34.150 回答
0

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

于 2012-06-26T16:22:51.697 回答