ExportViewModel Fails if....

Dec 21, 2010 at 11:38 PM
This suceeds with the param commented out..
[ExportViewModel("CurPersonRibbonTabViewModel")]
public class CurPersonRibbonTabViewModel 
{
    [ImportingConstructor]
    public CurPersonRibbonTabViewModel(/*IMediator m*/)
    {
    }
} 
This fails...
 
 
[ExportViewModel("CurPersonRibbonTabViewModel")]
public class CurPersonRibbonTabViewModel 
{
    [ImportingConstructor]
    public CurPersonRibbonTabViewModel(IMediator m)
    {
    }
} 
 
With
System.ComponentModel.Composition Warning: 1 : The ComposablePartDefinition 
'CurPersonMaintenance.CurPersonRibbonTabViewModel' has been rejected. 
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) More than one export was found that matches the constraint 
'((exportDefinition.ContractName == "MEFedMVVM.Services.Contracts.IMediator") 
AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") 
AndAlso "MEFedMVVM.Services.Contracts.IMediator".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'.

Resulting in: Cannot set import 
'CurPersonMaintenance.CurPersonRibbonTabViewModel..ctor (Parameter="m", ContractName="MEFedMVVM.Services.Contracts.IMediator")' 
on part 'CurPersonMaintenance.CurPersonRibbonTabViewModel'.
Element: CurPersonMaintenance.CurPersonRibbonTabViewModel..ctor 
(Parameter="m", ContractName="MEFedMVVM.Services.Contracts.IMediator") -->  
CurPersonMaintenance.CurPersonRibbonTabViewModel -->  
DirectoryCatalog (Path="C:\Users\stevej\documents\visual studio 2010\Projects\WpfSample1\WpfRibbonSample\bin\Debug\Plugins\")

Any thoughts?
Thanks in advance,
Steve Johnson
 

 

 
Dec 22, 2010 at 12:53 AM
Edited Dec 22, 2010 at 12:53 AM

From the exception message you can tell there are multiple implementors of IMediator. MEF doesn't know which one to pick, so it rejects the composition.

You should either have a single implementor, or change the constructor to import all of them: [ImportMany] IEnumerable<IMediator> mediators

 

Blog post on this: http://blogs.msdn.com/b/hammett/archive/2010/09/27/mef-gems-1-of-many-import-vs-importmany.aspx

Dec 22, 2010 at 4:15 AM

I did more research and saw that this was the problem, I'm just not quite sure how to solve it since I use MefedMVVM.WPF in the main program and each plugin.  I also seem to have the same problem with other service libraries that are used in multiple modules.  The example from MefedMVVM for multiple directories doesn't seem to share my problem.  I tried to mimick the way he did it, but somehow I'm missing something.  ImportMany won't work in the case of the IMediator since all the ViewModels should share the same instance so they can communicate. 

Thanks for the response haveriss,

Steve

Dec 22, 2010 at 7:29 PM

I seem to have solved this, but I'm not sure.  My MainWindow originally had a ViewModel that was Meffed in by MefedMvvm.  As soon ass I removed it all was good.  I never did figure out why though.