MEF composition issue
I finally found the problem, and it had nothing to do with the CalypsoBookSelectorViewModel that MEF was pointing the finger on.
Indeed, the ViewModel has dependencies on another component (a CalypsoBookSelectorModel), which in turn has a dependency on a IDispatcher component.
The problem was that this IDispatcher component, which was specified with a contract name (see below), was exported TWICE (once in each plugin), so MEF couldn't tell which one to use. The real problem of course is that MEF should have told me that, instead of pointing the finger to a class two levels up the chain.
Thanks Dennis for looking at the problem, and I hope this will help other people who'll get the same problem.
The Dispatcher import:
[Import(DispatcherNames.BackgroundDispatcherName, typeof(IDispatcher))]
public IDispatcher Dispatcher { get; set; }
Your P1
imports something from C
(more exactly, ICalypsoBookSelectorViewModel
).
When MEF container tries to create P1
, it also tries to resolve all imports, which P1
depends from. Hence, it performs search for export of ICalypsoBookSelectorViewModel
type (indeed, contract name, but it doesn't matter in this case) in its own catalog and parent export providers.
If such export is not found (this is your case), MEF container remains composition unchanged.
To fix this, you should add [Export(typeof(ICalypsoBookSelectorViewModel))]
to the corresponding type definition.
Of course, all this implies, that your catalog and export providers (if there are any) are initialized properly.
Note, that this export definitions are not equal:
public interface IA {}
[Export(typeof(IA))] // contract name is "IA"
public class A : IA {}
[Export] // contract name is "A"
public class A : IA {}
[Export]
public class Composed
{
[Import] // MEF will search for exports like [Export(typeof(IA))]
private IA field1;
[Import] // MEF will search for exports like [Export]
private A field1;
}