IModuleManager.LoadModule API

Topics: Prism v2 - Silverlight 2
Jun 24, 2009 at 3:46 PM

I'm writing some code that does dynamic module loading. The IModuleManager.LoadModule API can load modules asynchronously which is all good, but there is no async pattern to let me know when this operation is complete. I can't find any event that provides this information.

The code I'm trying to write is to implement an internal uri based navigation scheme within my app. So imagine I have a uri that looks something like:

/{app stuff}/{module_id}/{stuff for module}

And I have some parsing code that walks down this path handing components the next part of the uri they are responsible for. When I get to the 'module_id' part I load the module and then I want to hand it (or some object implemented inside it) the rest of the path.

I saw another post where it was suggested that this data get stored off somewhere and the IModule::Initialize code would then look for it. The idea being that this keeps things losely coupled. I could do something like this, but I also have a use case where the above url parsing scheme comes a long and the module has already been loaded so its Initialize won't (and can't) be called again.

Ideally I would want some kind of callback when module loading is complete or fails. Is there something like that available that I'm just not seeing? Or is there a design reason why the module loading API doesn't provide this? I was also looking for something that could tell me if a module has been loaded, or is in the process of being loaded and all I could come up with is a query:



<font size="2">




// See if module is already loaded.

<font size="2">




ModuleInfo moduleInfo = this.moduleCatalog.Modules.Where(m => (m.State == ModuleState.NotStarted && m.ModuleName == id)).FirstOrDefault();




Jun 24, 2009 at 8:09 PM

Hi John,


Currently, there is no functionality that allows you to receive a callback in your application (an implementation of this might be a useful idea for some situations) when a particular module has been loaded or failed to be. I do not know your exact scenario, but in a similar thread we discussed how to pass startup args to the module (perhaps this was the one you mentioned). As you said, once the module has been loaded its execute method is not executed again.


When you say “I also have a use case where the above url parsing scheme comes a long”, I understand that another call is performed in which you need some data from this shared service to be able parse the URL. In this case, you could also access the Service’s instance (mentioned in the link above) through your container and get the required information from it where you need to perform this parsing.


As I said before, I do not know the specifics of your application, but always remember that when sharing information globally (for all modules) in your application you should check that you are keeping it loosely coupled.


To check if a module has been loaded the way you are using is correct. You could also get the state of the Module directly and check its status to make a particular decision (through an if or switch statement).


The following post provides some more information about the Module Initialization Lifecycle. You might find it useful:

·         Enhancing the Prism Module Initialization Lifecycle


Please let me know if this helps.


Damian Schenkelman

Jul 3, 2009 at 7:45 PM

Thanks Damian! Sorry it took me a bit to get back to you...

The thread you linked to is what I was referring to. My plan for implementing this will be to save the paramters I want to pass to the module (or an object inside it). Then when the module loads it will fire an event indicating that it has initialized. The responder of that event will then obtain a reference to the module (perhaps the module can pass this as the event payload) and call a method on the interesting object in the module to tell it what it needs to finish initializing.

Imagine a scenario where a module can present data in different modes and I want to load it and tell it to present in a particular mode. I think that the above plan will work.

However, I do think that it is a major deficiency in the API that there is no way to find out that a module has failed to load. I'm planning on loading the dll dynamically from my host web site and we all know that this could fail for a client at any moment. Applications need to know this so that it can try to recover. I may create a patch myself to provide this functionality for my app because I think it is important.

Thanks again for your help.