This project has moved and is read-only. For the latest updates, please go here.

Single Extension loaded twice?

Mar 22, 2010 at 8:31 PM

Good evening,

I've just noticed another strange behaviour with my sample/playground MEF project (

Whenever I e.g. add the 'MefExample.Extensions.ConsoleLogger.dll' file into the 'MefExample.HostApplication\bin\Debug\Extensions\' folder and start the HostApplication, all .log(message) calls are run twice.. because the LogManager.Loggers property contains two (?) instances of the ConsoleLogger mentioned above. But why's that? one .dll with one ILogger implementation/export gets added to the Loggers / ImportMany Property twice? How can I prevent that or whats going on?

Cheers and thanks,


Mar 22, 2010 at 10:51 PM

The problem is because LoggerMetadataAttributes is an ExportAttribute:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class LoggerMetadataAttribute : ExportAttribute

It is fine and good practice to make this attribute inherit from ExportAttribute but when you do that you should only apply the LoggerMetadataAttribute to your Exports you don't need to also apply the ExportAttribute because if you do you are essentially exporting it twice.

Also as I realize this is sample code I did notice you doing the following:


[ImportMany(AllowRecomposition = true)]
public List<ILogger> Loggers { get; private set; }


compositionBatch = new CompositionBatch();
directoryCatalog = new DirectoryCatalog(extensionsDirectory);
compositionContainer = new CompositionContainer(directoryCatalog);

Loggers = compositionContainer.GetExportedValues<ILogger>().ToList();

Now there are a couple things that are interesting here, one you are creating an empty batch and composing it which you don't need to do, two you are manually pulling on the container as well as having an ImportAttribute on the property which is never actually used. My suggestion would be to remove the batch and the manual initialization line and call compositionContainer.ComposeParts(this) which will cause the import to be satisfied. 



Mar 22, 2010 at 11:35 PM

Hello there,

Thanks a lot for the heads-up.. it all works perfectly fine now! :)