Problem loading modules on demand

Topics: Prism v4 - Silverlight 4
Oct 17, 2011 at 4:51 PM
Edited Oct 17, 2011 at 5:54 PM

I am developing a modular silverlight application with MEF. Up to this point my module catalog had specified each module to load automatically (WhenAvailable). I then changed the modules to load OnDemand and used the ModuleManager to manually load the modules. I am getting the following error:

Unable to locate the module with type 'MyCompany.MyProject.Module1.Module1Module, MyCompany.MyProject.Module1, Version=, Culture=neutral, PublicKeyToken=null' among the exported modules. Make sure the module name in the module catalog matches that specified on ModuleExportAttribute for the module type.

I get this error for each module I attempt to manually load. However, the modules are loading fine because I can still navigate to the views within the module XAP.

My module classes are declared in a regular way:

public class Module1Module : IModule

I also tried specifying InitializationMode.OnDemand in the ModuleExportAttribute.

The MefModuleManager.OnModuleLoaded event is not raised even though the modules are being loaded.

It seems like MEF may be failing on some other import and not the module class itself, but I don't see how. None of my modules register any services. Right now they all contain only views and viewmodels.

Again, everything seems to work fine loading them OnDemand except I get the error.

Any ideas?

Oct 17, 2011 at 8:51 PM


You might find the following thread useful where a similar behavior is described:

Also, you can check the Modularity Quickstart with Silverlight included in the Prism guidance where it's shown how to load modules on demand when using MEF.

If the aforementioned links where not useful to solve this, you could sent us a repro-sample application portraying this problem so we could analyze it and help you find the reason behind this undesired behavior.

I hope you find it useful,

Damian Cherubini

Oct 18, 2011 at 4:12 PM
Edited Oct 18, 2011 at 4:13 PM

The exception is being thrown from Prism Library: MefModuleInitializer.CreateModule(ModuleInfo moduleInfo)

protected override IModule CreateModule(ModuleInfo moduleInfo)
            // If there is a catalog that needs to be integrated with the AggregateCatalog as part of initialization, I add it to the container's catalog.
            ComposablePartCatalog partCatalog;
            if (this.downloadedPartCatalogs.TryGet(moduleInfo, out partCatalog))
                if (!this.aggregateCatalog.Catalogs.Contains(partCatalog))


            if (this.ImportedModules != null && this.ImportedModules.Count() != 0)
                Lazy<IModule, IModuleExport> lazyModule =
                    this.ImportedModules.FirstOrDefault(x => (x.Metadata.ModuleName == moduleInfo.ModuleName));
                if (lazyModule != null)
                    return lazyModule.Value;

            // This does not fall back to the base implementation because the type must be in the MEF container and not just in the application domain.
            throw new ModuleInitializeException(
                string.Format(CultureInfo.CurrentCulture, Properties.Resources.FailedToGetType, moduleInfo.ModuleType));

The first part of the function works fine. It gets the parts from each module and adds them to the aggregate catalog. That is why my views and viewmodels are working despite the error. The problem is this.ImportedModules has 0 count and so it throws the exception.

        [ImportMany(AllowRecomposition = true)]
        public IEnumerable<Lazy<IModule, IModuleExport>> ImportedModules { get; set; }

My module definition class is clearly there in the partCatalog that was downloaded, but ImportedModules is not being updated...

Image and video hosting by TinyPic

Oct 18, 2011 at 5:19 PM


It would be helpful if you could provide us with a sample application that portrays this issue in isolation, so we can help you understand its causes and provide a possible solution or workaround for it.


Guido Leandro Maliandi

Oct 18, 2011 at 6:26 PM


I was able to find the cause of the problem.

I am using MEFedMVVM library and it has its own CompositionContainer. So I needed to register the IModuleManager export with this container.

The error was NOT prism related, so you can delete this thread. Thank you.