Search code examples
c#importexportmef

MEF Container cannot compose parts from Shared Assembly


I have 3 projects in my solution:

1- ExporterLib (Class Library)

2- ImporterApp (WPF Application)

3- SharedLib (Class Library)

the first two projects reference "SharedLib" which contains an interface called ISharedClass .

Inside "ImporterApp" I have a class called ClassA:

public class ClassA
{
    [Import] private ISharedClass part;
}

Inside "ExporterLib" I have a class called SharedClassExport:

[Export(ISharedClass)]
public class SharedClassExport : ISharedClass
{
    //....
}

In ImporterApp I use a DirectoryCatalog referencing ExporterLib.dll file and a container. But when I try to compose the parts of ClassA instance using the container I receive an exception saying:

1) No exports were found that match the constraint: ContractName SharedLib.ISharedClass RequiredTypeIdentity SharedLib.ISharedClass

When I use the debugger to see the parts inside the catalog I see the correct SharedClassExport Part, but it's not referencing ISharedClass!

What Should I Do?

P.S. : I want to use MEF and I don't want to merge any of these projects together.

Thanks in advance.


SUMMARY:

//Specifying the contract type may be important if you want to export a type other then the base type, such as an interface.

https://msdn.microsoft.com/en-us/library/system.componentmodel.composition.importattribute(v=vs.110).aspx


Solution

  • Please try using [Export(typeof(ISharedClass))] . If you are exporting a type other than a base type, such as interface, it is important to specify the full contract type using typeof. To match the export use [Import(typeof(ISharedClass))]. Please let me know if this worked if not please provide the DirectoryCatalog composition logic you are using?