Importing IModuleManager Inside a Module

Topics: Prism v4 - Silverlight 4
May 25, 2011 at 2:14 PM

Hi,

I'm using Prism 4 with MEF on a Silverlight application.

One of my Modules has the following imports:

[ModuleExport(typeof(ModuleInit))]
public class ModuleInit : IModule

private IModuleManager _moduleManager;

[ImportingConstructor]
public ModuleInit(IModuleManager moduleManager)
{
    _moduleManager = moduleManager;
}

...

But it looks like the IModuleManager is not available yet, as I get the following exception (the marked line is bottstrapper.Run()):

GetExportedValue cannot be called before prerequisite import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor (Parameter="moduleInitializer", ContractName="Microsoft.Practices.Prism.Modularity.IModuleInitializer")' has been set.
Resulting in: An exception occurred while initializing module 'ModuleInit'.     - The exception message was: GetExportedValue cannot be called before prerequisite import 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager..ctor (Parameter="moduleInitializer", ContractName="Microsoft.Practices.Prism.Modularity.IModuleInitializer")' has been set.    Check the InnerException property of the exception for more information. If the exception occurred     while creating an object in a DI container, you can exception.GetRootException() to help locate the     root cause of the problem. 
Resulting in: An exception occurred while calling the 'OnImportsSatisfied' method on type 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.
Resulting in: Cannot activate part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.Element: Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager -->  Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager -->  AssemblyCatalog (Assembly="Microsoft.Practices.Prism.MefExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
Resulting in: Cannot get export 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager (ContractName="Microsoft.Practices.Prism.Modularity.IModuleManager")' from part 'Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager'.Element: Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager (ContractName="Microsoft.Practices.Prism.Modularity.IModuleManager") -->  Microsoft.Practices.Prism.MefExtensions.Modularity.MefModuleManager -->  AssemblyCatalog (Assembly="Microsoft.Practices.Prism.MefExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

 

Removing the IModuleManager import from this Module or setting it as Lazy<IModuleManager> will solve the problem, but I wanted to know the reason for the exception

And perhaps understand what am I doing wrong.

Thanks!

May 26, 2011 at 2:59 PM

Hi,

We’ve been able to reproduce the behavior you’re mentioning. The behavior you are experiencing could be caused by a timing issue during the composition in your application. According to the error messages provided, it looks like the IModuleManager Import can’t be satisfied until the IModuleInitializer composable part has been composed.

One possible workaround for this could be to use the ServiceLocator to resolve an instance of IModuleManager, after your exported component has been fully loaded in order to avoid this unexpected behavior (for example, in reaction to a button in a component of yours).

On the other hand, if you think this timing issue is not desire, you can create an issue in the Issue Tracker.

Thanks,

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