MEF Composition Problem

I can’t solve what seems to be the main problem of MEF: I have a “plugins” project (which we will call P1 and P2), and a third project that is common to both plugins (which we will call C). P1 and P2, as reference C.

When you try to import a component that lives in P1, it fails because this component has dependencies on components that live in C.

Here is the trace:

System.ComponentModel.Composition Warning: 1: ComposablePartDefinition 'MyCompany.Client.Pms.Plugin.InclusionList.ViewModel.InclusionListViewModel' was rejected. The composition remains unchanged. Changes were rejected due to the following error (s): the composition produced multiple composition errors with four root causes. The main reasons are as follows. See the CompositionException.Errors property for more information.

1) Export was not found, which corresponds to the restriction '((exportDefinition.ContractName = "MyCompany.Client.Plugins.Common.Controls.Selectors.PortfolioSelectors.ViewModel.ICalypsoBookSelectorViewModel") && & && & & &; (exportDefinition.Metadata.Metadata.Meteyata.Metadata.Meteyata.Meteyata.Meteyata.Meteyata.Meteyata.Meteyata.Meteyata.Meteyata. "ExportTypeIdentity") && & && & &&&&& "MyCompany.Client.Plugins.Common.Controls.Selectors.PortfolioSelectors.ViewModel.ICalypsoBookSelectorViewModel" .Equals (exportDefinition.Metadata.get_Item) "ExportType)

Result: Unable to install import. MyCompany.Client.Pms.Plugin.InclusionList.ViewModel.InclusionListViewModel.CalypsoBookSelectorViewModel (ContractName = "MyCompany.Client.Plugins.Common.Controls.Selectors.PortfolioSelectors.ViewModel.ICalypsoBooklmsPlomPlsBooksmslmspartlemslemspemsplsbooksmslsmslsplsbooksmslsmslsplsbooksmslsmslsmslsplsbooksmslemsplsbooksmslsmsmsplsbooksmslsmslsmslsmslsmslsmslsmslsmslsmsmslsmslsmslsmslslsmslsmslsmslsmsmslsmslsmslslsmslsmslsmsmsls InclusionList.ViewModel.InclusionListViewModel '. Element: MyCompany.Client.Pms.Plugin.InclusionList.ViewModel.InclusionListViewModel.CalypsoBookSelectorViewModel (ContractName = "MyCompany.Client.Plugins.Common.Controls.Selectors.PortfolioSelectors.ViewModel.ICalypsoBookMentlCommsPlomBooksComPlomBooksComPlientBook .ViewModel.InclusionListViewModel → DirectoryCatalog (Path = "C: \ Work \ mmtrader \ dashboard \ Code \ Src \ Dashboard \ MM \ Trader \ bin \ Debug \ Plugins \ Positions")

[...] (Three other problems are exactly the same on different viewing models)

I looked at the MEF directory and it turns out that MEF knows about these view models, so I don’t know what is missing.

As Dennis requested below, here is my import / export:

Export

Export(typeof(ICalypsoBookSelectorViewModel))] public class CalypsoBookSelectorViewModel : ScreenWithCleanupLifecycle, ICalypsoBookSelectorViewModel {...} 

Import

 [Import(typeof(ICalypsoBookSelectorViewModel))] public ICalypsoBookSelectorViewModel CalypsoBookSelectorViewModel { get; set; } 

And the directory:

Catalog

Thanks in advance for your help!

+8
c # composition mef
source share
3 answers

I finally found the problem, and it had nothing to do with the CalypsoBookSelectorViewModel that MEF was pointing to.

Indeed, the ViewModel has dependencies on another component (CalypsoBookSelectorModel), which, in turn, has a dependency on the IDispatcher component.

The problem was that this IDispatcher component, which was specified with the name of the contract (see below), was exported by TWICE (once in each plugin), so the MEF could not determine which one to use. The real problem, of course, is that MEF should have told me that instead of pointing a finger at class level 2 up the chain.

Thanks to Dennis for looking at the problem, and I hope this helps other people who get the same problem.

Import Manager:

 [Import(DispatcherNames.BackgroundDispatcherName, typeof(IDispatcher))] public IDispatcher Dispatcher { get; set; } 
+8
source share

Your P1 imports something from C (more precisely, ICalypsoBookSelectorViewModel ).

When the MEF container tries to create P1 , it also tries to allow all imported ones that P1 depends on. Therefore, it searches for an export of type ICalypsoBookSelectorViewModel (indeed, the name of the contract, but in this case it does not matter) in its own directory and parent export providers.

If such an export is not found (this is your case), the MEF container remains unchanged.
To fix this, you should add [Export(typeof(ICalypsoBookSelectorViewModel))] to the corresponding type definition.

Of course, all this means that your catalog and export providers (if any) were correctly initialized.

Note that these 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; } 
+6
source share

Ocurr error because it uses [Import] , to import more than one you must use [ImportMany]

0
source share

All Articles