Sunday 13 April 2014


Share/Bookmark
Issue:

While in the process of module initialization, if there is any error in a module, then PRISM will throw exception and will stop loading other modules until you fix the error in the module. But if your module is not really important in your application, you would like to suppress this module initialization error and continue loading other modules.


How did I fix it:

In such situation you have to handle the ModuleInitializationError by your own.
This is achieved by a Custom module initializer that silently logs module initialization errors but continues execution without throwing exceptions further up the call stack.

You will need to inherit ModuleInitializer and override HandleModuleInitializationError and then handle the exception by your own.
 ModuleInitializer is available in Microsoft.Practices.Prism.Modularity;

public class CustomModuleInitializer : ModuleInitializer
    {
        public CustomModuleInitializer(IServiceLocator serviceLocator, ILoggerFacade loggerFacade)
            : base(serviceLocator, loggerFacade)
        {
        }

        public override void HandleModuleInitializationError(ModuleInfo moduleInfo, string assemblyName, Exception exception)
        {
            try
            {
                base.HandleModuleInitializationError(moduleInfo, assemblyName, exception);
            }
            catch (Exception ex)
            {
//log errors.
            }
        }
    }

And from your bootstrapper, override ConfigureContainer and register this module initializer like as shown below.
protected override void ConfigureContainer()        
{
// Register custom module initializer that does not stop initializing modules on first error.            
Container.RegisterType<IModuleInitializer, CustomModuleInitializer>();
}
Categories: , ,

0 comments:

Post a Comment

Dear reader, Your comment is always appreciated. I will reply to your queries as soon as possible.

1. Make sure to click on the Subscribe By Email link to be notified of follow up comments and replies. Or you can use Subscribe to: Post Comments (Atom) link.
2. Only English comments shall be approved.
3. Please make your comments self-explanatory. Comment in such a way that it explains the comment so that a counter question can be avoided and can be replied with proper answer on the first go.