Search code examples
c#wpfmef

No exports were found that match the constraint: Issues getting my wpf application to work with MEF


I am working on a WPF application and I want to architect my project using MEF (Manageable Extensibility Framework). But the problem is that I am getting this error when I try to run my application:

No exports were found that match the constraint: ContractName MyFooPluginA RequiredTypeIdentity namespace.of.my.core.project.IFooPlugin

Here is what I went ahead and created

enter image description here

The same concept applies to all plugin projects of type IBarPlugin. Here is how I am setting up my first plugin project:

The FooView.xaml.cs

    /// <summary>
    /// Interaction logic for FooView.xaml
    /// </summary>
    [Export(typeof(IFooPlugin)), PartCreationPolicy(CreationPolicy.Any)]
    [ExportMetadata("Name", "MyFooPluginA")]
    public partial class FooView: UserControl, IFooPlugin
    {
        [ImportingConstructor]
        public FooView(FooViewModel viewModel) //we initialize the view first, then the view model
        {
            InitializeComponent();

            DataContext = viewModel;
        }

    }

The FooViewModel:

[Export]
public class FooViewModel
{
  [ImportingConstructor]
  public FooViewModel(...) //Contains parameters for dependency injections
  {
    //doing some work here
  }
}

Finally in the main application view model I am loading the plugins:

public class MainAppViewModel 
{  
        /// <summary>
        /// If one plugin of type IFooPlugin is found then it is loaded in this property
        /// </summary>
        public IFooPlugin FooPluginView
        {
            get
            {
                return _fooPluginView;
            }
            set
            {
                _fooPluginView= value;
                RaisePropertyChanged(nameof(FooPluginView));
            }
        }    
        private IFooPlugin _fooPluginView;

        /// <summary>
        /// Stores the catalog of all exported dlls
        /// </summary>
        private AggregateCatalog catalog;

        /// <summary>
        /// Stores the catalog information and all its parts
        /// </summary>
        private CompositionContainer Container;

        public MainAppViewModel()
        {
          InitPlugin();//first thing I want it to do

          if (someConditon == true)
          {
            FooPluginView = Container.GetExport<IFooPlugin>("MyFooPluginA").Value;
          }
          else
          {
            FooPluginView = Container.GetExport<IFooPlugin>("MyFooPluginA").Value;
          }
       }

       private void InitPlugin()
       {
            //First create a catalog of exports
            //It can be TypeCatalog(typeof(ISomeView), typeof(SomeOtherImportType)) 
            //to search for all exports by specified types
            //DirectoryCatalog(pluginsPath, "App*.dll") to search specified directories
            //and matching specified file name
            //An aggregate catalog that combines multiple catalogs
            catalog = new AggregateCatalog();

            //Here we add all the parts found in all assemblies in directory of executing assembly directory
            //with file name matching Plugin*.dll
            string pluginsPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            catalog.Catalogs.Add(new DirectoryCatalog(pluginsPath, "*Plugin.dll"));

            //also we add to a search path a subdirectory plugins
            pluginsPath = Path.Combine(pluginsPath, "Plugins");

            catalog.Catalogs.Add(new DirectoryCatalog(pluginsPath, "*Plugin.dll"));

            //Create the CompositionContainer with the parts in the catalog.
            Container = new CompositionContainer(catalog);

            //Fill the imports of this object
            //finds imports and fills in all preperties decorated
            //with Import attribute in this instance
            Container.ComposeParts(this);
     }
 }

Solution

  • I'm afraid this may not be the exact answer you want, But as far as I know Attribute values of [ExportMetadata] does not setting the ContractName. It's values are assigned as Metadata. So, you can change

    FooPluginView = Container.GetExport<IFooPlugin>("MyFooPluginA").Value;

    to

    FooPluginView = Container.GetExports<IFooPlugin, IDictionary<string, object>>().Where(x => (string)x.Metadata["Name"] == "MyFooPluginA").FirstOrDefault().Value;

    or similar. Also, It seems you didn't passed parameter to ImportingConstructor. I tried solution which explained in MEF Constructor Injection to your case like

    [ImportingConstructor]
    public UserControl1([Import("DI")]FooViewModel viewModel)
    
    Container.ComposeExportedValue("DI",new FooViewModel(null));
    FooPluginView = Container.GetExports<IFooPlugin, IDictionary<string, object>>().Where(x => (string)x.Metadata["Name"] == "MyFooPluginA").FirstOrDefault().Value;
    
    

    and it works great in my environment. I hope this might help you.