Duplicate imports when using Lazy and Metadata

Feb 1, 2010 at 2:00 PM

I have a very simple project (VS2010 .Net 4.0), I have DataRepository class that has a property

<font face="Consolas" size="2"><font face="Consolas" size="2">

[

</font></font><font face="Consolas" size="2">

 

</font>

ImportMany]private Lazy<IDataRepositoryProvider, IDataRepositoryProviderCapabilities>[] Providers { get; set; }

 

I have one implemtation of IDataRepositoryProvider.  At startup I create a DataRepository and call ComposeParts

 

var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
var argCat = new AggregateCatalog(catalog);
var container = new CompositionContainer(argCat);
var dr = new DataRepository () ;
container.ComposeParts(dr);

At this point Provider now contains 2 items.  While the array contains two different Lazy objects, the values created apear to be the same object.

If I remove the metadata from the property, then it only contains a single item.

Anyone know what's going on?

Cheers.

Stephen.

 

Developer
Feb 1, 2010 at 4:09 PM

Can you post the code for the metadata attribute and the class you are exporting?

I suspect that you are writing a custom metadata attribute which inherits from ExportAttribute, in which case you are essentially putting two ExportAttributes on your type. The fix for this is to simply use the custom export attribute and remove the plain ExportAttribute.