简体   繁体   中英

MEF export of Repository<T> : IRepository<T>

I am trying to use MEF to Export the following:

[Export(typeof(IRepository<>))]
public class Repository<T> : IRepository<T>
    where T : class
{

With an import of

[Import(typeof(IRepository<>))]
private IRepository<Contact> repository;

But I keep getting an error message when composing MEF of:

=========================================

The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No valid exports were found that match the constraint '((exportDefinition.ContractName = "Interfaces.IRepository()") && (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && "Interfaces.IRepository()".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.

Resulting in: Cannot set import 'SoCLINQ2SQL.RepositoryTest.repository (ContractName="Interfaces.IRepository()")' on part 'SoCLINQ2SQL.RepositoryTest'. Element: SoCLINQ2SQL.RepositoryTest.repository (ContractName="Interfaces.IRepository()") --> SoCLINQ2SQL.RepositoryTest

To the best of my knowledge, and according to Glenn Block's post on the subject, MEF does not support open generic types "out of the box".

Apparently there is support for it in the MEF contrib project .

I believe in that case you would be able to leave your export as an open generic type but on the import side you would need to change your import to look like:

[Import(typeof(IRepository<Contact>))]
private IRepository<Contact> repository;

I had a similar problem, and it was related to the order on which the assemblies were added to the AggregateCatalog . The example below illustrates the Bootstrapper.ConfigureAggregateCatalog() . "Module B" was invoking a service from "Module C" , but "Module C" wasn't added to the AggregatorCatalog yet. I just had to change the order, and it solved to problem.

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();


 // Be aware of the order on which the assemblies are added to the AggregateCatalog.
 // It's important to add the assembly to the AggregateCatalog in the correct order, otherwise
 // you may get the error "No valid exports were found that match the constraint".
 // In the example below, if Module B invokes a method of Module C, module C must be
 // added to the AggregateCatalog prior to Module B. 
 // Please note the Bootstrapper assembly also needs to be added to the AggregateCatalog.
 // -------------------------------------------------------------------------------------- 
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB).Assembly));

    }

Just a FYI: this is supported in the upcoming version. A preview drop should be in our codeplex site soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM