Where to catch ModuleManager.LoadModule exceptions?

Topics: Prism v2 - Silverlight 3
Nov 16, 2009 at 11:54 PM

I have a modular application, which loads some modules at runtime. I am using Prism v2 w/ Silverlight 3. I have verified that the modules load OK.

Now, if a module is missing from the server, Prism throws the ModuleTypeLoadingException. I tried catching the exception at several points:

e.g.

 try
{
_moduleManager.LoadModule(_currentColorTheme.Name);
}
catch (Exception ee)
{
Debug.WriteLine("Loading module failed.");
}

However, I can't catch the exception in my code (it eventually gets caught in Application_UnhandledException but that's too high up the chain for me and not ideal).

Is there a recommended approach for catching exceptions originating in the Prism assemblies?

Thanks,

Alex

Nov 17, 2009 at 1:31 AM

I didn't digged up the source for the LoadModule method, but I assume that it's an async method, that's why you can't put it into a try..catch.

What should be a help here is that in the Application_UnhandledException raise an event for handling the exception and pass an action with the event which should be called if none of the subscribers handled the raised exception.

This way, your exception handling is happening in an async fashion too ;-)

Meanwhile I digged up the source :-))) as I see your another choice is to derive your own class from ModuleManager and override the HandleModuleTypeLoadingError method which ones 2nd method is the exception occured during module loading. From the overridden method you should also raise an event which can be handled in the appropriate part of your application.

Hope that helps,

Attila

Nov 30, 2009 at 11:17 PM

Attilah,

Thank you for your response and sorry it took me a while to answer (was out of the country and in the woods). I implemented your 2nd idea, overrode the HandleModuleTypeLoadingError() method and it worked beautifully. I like how the Prism team has designed the extensibility points. As it turns out, they're quite useful. Thanks again, Attilah!