Then, I add each and every directory to the DirectoryCatalog
AFAIK it is not possible to add multiple directories to a DirectoryCatalog. A DirectoryCatalog corresponds to exactly one directory, i.e. DirectoryCatalog.FullPath. Therefore I'll assume that you are actually adding multiple DirectoryCatalog instances to
and finally on composition I have multiple instances of the same assembly.
I see three possible solutions:
1. Get rid of the duplicate assemblies.
2. Don't use DirectoryCatalog. Instead, scan the directories yourself and create an AssemblyCatalog for each assembly that you want to include. You could include some logic to filter out duplicates. Finally, add each AssemblyCatalog
to a single AggregateCatalog.
3. Create separate export providers for each directory catalog. The order of the export providers will determine their priority in the container. For example:
using (var catalog1 = new DirectoryCatalog(...))
using (var catalog2 = new DirectoryCatalog(...))
provider1 = new CatalogExportProvider(catalog1);
provider2 = new CatalogExportProvider(catalog2);
using (var container = new CompositionContainer(provider1, provider2)
provider1.SourceProvider = container;
provider2.SourceProvider = container;
var program = container.GetExportedValue<Program>();