MEFX shows Rejection on PRISM Imports IServiceLocator

Topics: Prism v4 - Silverlight 4
Mar 20, 2011 at 11:56 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 21, 2011 at 7:04 PM

Hi,

A possible cause for the issue you’re experiencing could be that you’re not adding the assembly catalog of your modules into the Aggregate Catalog, therefore the exports placed in these modules are not reaching the container. You could try adding a reference to each of your module’s projects into your Silverlight Application Project and also adding a code similar to this in your bootstrapper class:

protected override void ConfigureAggregateCatalog()
{
      base.ConfigureAggregateCatalog();
 
      this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MyBootstrapper).Assembly)); 
 
      // Module A is referenced in in the project and directly in code.
      this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA.ModuleA).Assembly)); 
}

 

Note that you should also include the assembly catalog of your bootstrapper in the override of that method, so that the exports defined in your shell project are also exported.

As for the override of the ConfigureContainer method you’ve done, if you don’t call base.ConfigureContainer in the overrided ConfigureContainer method of your Bootstrapper, the ServiceLocator won’t be registered into the container. In the MefBootstrapper class the ConfigureContainer method is calling the RegisterBootstrapperProvidedTypes method which internally is doing the following:

this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));

 

This could be the reason why you are experiencing an exception when you try to compose the ServiceLocator.

You might find the following resources useful, where modularity in Silverlight is covered:

If you keep experiencing problems after implementing the aforementioned suggestions, it would be helpful if you could provide us with a repro sample of your solution, so that we can help you further with your issue.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic