How to suppress PRISM module initialisation errors and continues execution

Introduction

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 do 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>();
}

Setting Up Kafka

Apache Kafka is a high-throughput, low-latency event processing system designed to handle millions of data feeds per second. It has the c...… Continue reading

Libish Varghese Jacob

Libish Varghese JacobI am a lead engineer at a prominent wind turbine manufacturing firm. My interests span a diverse range, and immersing myself in technology is one of them. This platform serves as my primary knowledge base, where I seek information and insights. Moreover, I utilize this platform to share my experiences and experiments, hoping they may benefit those following a similar path. It's important to note that the suggestions I express here are based on my best knowledge at the time of writing and may not necessarily represent the optimal solutions. I wholeheartedly welcome any comments you may have to improve or refine my ideas. Your feedback is greatly appreciated and valued.