MEFX & Primary Rejections on PRISM assemblies

Mar 20, 2011 at 5:29 PM

I had a wonderful little PRISM application that was working wonderfully well using the MEF container. In my first revision, had all my modules purposely situated in the main SilverLight client application with the intention to migrate each one to their own separate assemblies at some point in the future.  I have now migrated all my modules into separate SilverLight class libraries and have everything compiling without a single warning. 

At runtime I am getting the following exception;

System.ComponentModel.Composition.ImportCardinalityMismatchException was unhandled by user code
  Message=No valid exports were found that match the constraint '((exportDefinition.ContractName == "AUBPOS.Assets.QPMControls.Client.ShellView") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "AUBPOS.Assets.QPMControls.Client.ShellView".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.

Of course I know that this exception is likely occuring as a result of an inner dependancy so I downloaded the mefx.Client.SilverLight tool to see what I could learn.

Following the import I do not see a single error with respect to my projects however I do see that all of my custom region adaptors are showing warnings, each one points back (through the Show button) to the Microsoft.Practices.Prism.MefExtensions.Regions.MefRegionBehaviorFactor with the message ... The part providing the export is rejected because of other issues.

Upon clicking on the Show button, it takes me to one of a number of Prism parts that show as error, in this case to the Microsoft.Practices.Prism.MefExtensions.Regions.MefRegionBehaviorFactor with the issue appearing to be related to the Imports area where it appears that the ctor parameter for "serviceLocator" in ContractName ="Microsoft.Practices.ServiceLocation.IServiceLocator is failing to resolve.

Also ... all of my Custom Region adaptors show Yellow (warning) and all point back to the issue of the serviceLocator as well,  I have these other MEFX errors which all contain an issue with the IServiceLocator ... 

 I see errors in MEFX on the MefModuleManager  where under Imports the ExactlyOne rule fails for parameter "moduleCatalog" (IModuleCatalog),  as well as parameter "loggerFacade" (ILoggerFacade)

I see errors in MEFX on the MefModuleInitializer  where under Imports the ExactlyOne rule fails for parameter "aggregateCatalog" (AggregateCatalog),  as well as parameter "loggerFacade" (ILoggerFacade) and  "serviceLocator" in ContractName ="Microsoft.Practices.ServiceLocation.IServiceLocator

I see errors in MEFX on the MefRegionViewRegistery  where under Imports the ExactlyOne rule fails on constructor parameter "serviceLocator" in ContractName ="Microsoft.Practices.ServiceLocation.IServiceLocator

I see errors in MEFX on the MefRegionNavigationService  where under Imports the ExactlyOne rule fails on constructor parameter "serviceLocator" in ContractName ="Microsoft.Practices.ServiceLocation.IServiceLocator

I see errors in MEFX on the MefNAvigationContentLoader where under Imports the ExactlyOne rule fails on constructor parameter "serviceLocator" in ContractName ="Microsoft.Practices.ServiceLocation.IServiceLocator

To help me understand what was going on I tried to add the following override in my MefBootStrapper ...

 protected override void ConfigureContainer()
       {
           Assembly.GetExecutingAssembly();
           Container.ComposeExportedValue(Logger);
           Container.ComposeExportedValue(ModuleCatalog);
           Container.ComposeExportedValue(Container);
           Container.ComposeExportedValue(AggregateCatalog);

           var serviceLocator = this.Container.GetExportedValue<IServiceLocator>();
           ServiceLocator.SetLocatorProvider(() => serviceLocator);
       }

Which throws an exception on the second last line when attempting to get the IServiceLocator from the container.
I am not certain that this is the root-cause of my issues, but I would love to see all green within MEFX.  I feel it may be unrelated, but would like to understand why IServiceLocator is causing so many errors.
Any help would be greatly appreciated.
Mar 22, 2011 at 3:53 AM

Although I am still seeing the above errors I was able to resolve my issues within my application and it is running fine now. 

Jun 6, 2011 at 4:57 PM

How was it resolved?

Aug 11, 2011 at 3:42 PM

This is caused by the mefbootstrapper registering items (IServiceLocator) programatically.  From the mef docs: http://msdn.microsoft.com/en-us/library/ff576068.aspx.

"In most cases, Mefx will be able to diagnose the root cause of a cascading failure. However, in cases where parts are added programmatically to a container, cases involving hierarchical containers, or cases involving custom ExportProvider implementations, Mefx will not be able to diagnose the cause. In general, the previously described cases should be avoided where possible, as failures are generally difficult to diagnose."

This makes prism apps a real pita to diagnose when you're using the mefbootstrapper.