Search code examples
c#-4.0mef

MEF - Composition Error in Release mode but not in Debug mode


I use the following code lines to initialize MEF container

       _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);

All the Parts belongs to one Assembly - no dependecies!

When I run the app in Debug mode everything goes wall,

But, when I switch to Release mode, I get CompositionException in that lines of code

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

I realized when I try to do a double call for this lines, for the first time I Call the method (from Watch window) I get CompositionException but second time it works out...

EDIT:

Here is how the AssemblySource.Instance looks like:

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}

Would love to have your assistance.


Solution

  • So, I found the problem. it is totaly wierd..

    The app is using NLog as logging framework.

    I have sepearate nlog configs for each environment (using XML Transformation from http://sedodream.com/)

    In the app.config.realese I set nlog attribute throwExceptions=false and that the cause of the bug!

    I have said totaly wierd..?

    Thanks