MEF - 发布模式下的合成错误,但不在调试模式下

时间:2012-04-01 15:37:02

标签: c#-4.0 mef

我使用以下代码行初始化MEF容器

       _mefContainer = new CompositionContainer(
                     new AggregateCatalog(
                     AssemblySource.Instance
                    .Select(x => new AssemblyCatalog(x))
                    .Where(AssemblyFilter)
                    .OfType<System.ComponentModel.Composition.Primitives.ComposablePartCatalog()));

        var batch = new CompositionBatch();
        batch.AddExportedValue<IWindowManager>(new WindowManager());
        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
        batch.AddExportedValue(_mefContainer);

        OnConfigure(batch);

        _mefContainer.Compose(batch);

所有Parts属于一个Assembly - 没有任何依赖!

当我在调试模式下运行应用程序时,一切都进入了墙,

但是,当我切换到发布模式时,我会在代码行中获得CompositionException

string contract = string.IsNullOrEmpty(key) ?  AttributedModelServices.GetContractName(serviceType) : key;
var exports = _mefContainer.GetExportedValues<object>(contract);

我意识到当我尝试对这一行进行双重调用时,我第一次调用方法(从Watch窗口)我得到CompositionException但是第二次它运行了...

修改

以下是AssemblySource.Instance的样子:

AssemblySource.Instance
Count = 24
[0]: {PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[1]: {System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[2]: {MOCH.Infrastrcture.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[3]: {Caliburn.Micro.Contrib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[4]: {Caliburn.Micro, Version=1.3.1.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f}
[5]: {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[6]: {MOCH.Bama.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[7]: {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[8]: {PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[9]: {System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[10]: {MOCH.Bama.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[11]: {WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[12]: {MOCH.Infrastrcture.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[13]: {WPFToolkit.Extended, Version=1.6.0.0, Culture=neutral, PublicKeyToken=3e4669d2f30244f4}
[14]: {GIS.Bama, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[15]: {MOCH.Infrastrcture.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[16]: {System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[17]: {MOCH.Bama.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[18]: {System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[19]: {System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
[20]: {MOCH.Bama.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
[21]: {System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35}
[22]: {Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
[23]: {MOCH.Bama.UI.Shell, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}

很想得到你的帮助。

1 个答案:

答案 0 :(得分:0)

所以,我发现了问题。这完全是奇怪的..

该应用程序使用NLog作为日志记录框架。

我为每个环境分配了nlog配置(使用来自http://sedodream.com/的XML转换)

在app.config.realese中我设置了nlog属性throwExceptions=false以及该错误的原因!

我说完全很奇怪..?

由于