How can ModuleInitializeException be handled?

Topics: Prism v4 - Silverlight 4
Nov 6, 2012 at 10:54 AM
Edited Nov 6, 2012 at 11:08 AM

Hello, I am trying to handle ModuleInitializeException (MIE) from my code but without success. It can't be done from my code because MIE is thrown in another thread. This thread initializated by callback of module loading over network.

So i need to alter prism sources to achieve possibility of handling MIE. From perspective 
of prism developer, how to do it better?


I think, it can be done by implementing custom IModuleInitializer, using it instead of default ModuleInitializer and firing some event through IEventAggregator to let graceful handling if this Exception somewhere in user code.

Or should Exception object additionally be passed in LoadModuleCompletedEventArgs of ModuleManager.LoadModuleCompleted?

Or somehow else?

Nov 6, 2012 at 5:12 PM
Edited Nov 6, 2012 at 5:26 PM


Based on my understanding both approaches should be valid for handling this kind of exceptions. In my opinion the more direct approach will be to implement a custom IModuleInitializer to replace the default implementation, which could override the HandleModuleInitializationError method in order to handle the exception. And for example this method could raise and event when this occurs as you mentioned.

On the other hand, I believe that passing the exception to the LoadModuleCompletedEventArgs when raising the LoadModuleCompleted event would be a more organized approach, as it will allow you to handle all the exceptions of the module loading process in a centralized fashion. For example, to achieve this, you could catch the exception and pass it, in the InitializeModule method in the ModuleManager class like this:

private void InitializeModule(ModuleInfo moduleInfo)
            ModuleInitializeException moduleInitializeException = null;

            if (moduleInfo.State == ModuleState.Initializing)
                catch (ModuleInitializeException e)
                    moduleInitializeException = e;
                moduleInfo.State = ModuleState.Initialized;

                this.RaiseLoadModuleCompleted(moduleInfo, moduleInitializeException);

The only problem, with this approach is that it will require creating a custom ModuleManager implementation, with a modified InitializeModule method (which is currently not defined as a virtual method). Hence, this would require copying the code of the default ModuleManager implementation in your custom one.


Agustin Adami

Nov 8, 2012 at 7:14 AM

Thank you for great detailed answer.

I've successfully created custom MefModuleManager, by merging ModuleManager/MefModuleManager class definitions, and using your handy snippet of InitializeModule.