{"record":{"id":"5cadc0be1d410806","repo":"PrismLibrary/Prism","slug":"an-error-occurred-while-initializing-the-modules","errorCode":null,"errorMessage":"An error occurred while initializing the Modules.","messagePattern":"An error occurred while initializing the Modules\\.","errorType":"exception","errorClass":"PrismInitializationException","httpStatus":null,"severity":"critical","filePath":"src/Maui/Prism.Maui/PrismAppBuilder.cs","lineNumber":228,"sourceCode":"        else if (errors.Count > 1)\n        {\n            throw new AggregateException(\"One or more errors were encountered while configuring the Module Catalog\", [.. errors]);\n        }\n\n        // Phase 2: Initialize modules so their services are registered\n        if (_container.IsRegistered<IModuleCatalog>() && _container.Resolve<IModuleCatalog>().Modules.Any())\n        {\n            try\n            {\n                logger.LogDebug(\"Initializing modules.\");\n                var manager = _container.Resolve<IModuleManager>();\n                manager.Run();\n                logger.LogDebug(\"Modules Initialized.\");\n            }\n            catch (Exception ex)\n            {\n                logger.LogError(ex, \"An error ocurred while initializing the Modules.\");\n                throw new PrismInitializationException(\"An error occurred while initializing the Modules.\", ex);\n            }\n        }\n        else\n        {\n            logger.LogDebug(\"No Modules found to initialize.\");\n        }\n\n        // Phase 3: Run OnInitialized delegates (module services are now available)\n        _initializations.ForEach(action =>\n        {\n            try\n            {\n                action(_container);\n            }\n            catch (Exception ex)\n            {\n                logger.LogError(ex, \"Error executing Initialization Delegate.\");\n                errors.Add(ex);","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/PrismAppBuilder.cs#L210-L246","documentation":"After the module catalog is configured, Prism runs each module's initialization (IModuleManager.Run). If a module's OnInitialized/RegisterTypes throws, Prism logs the error and rethrows it as a PrismInitializationException with this message. The app cannot finish startup.","triggerScenarios":"A registered module's IModule.Initialize or RegisterTypes throws during OnInitialized — e.g. resolving a missing service, throwing in module constructor, or module logic that depends on unavailable platform services.","commonSituations":"Module relying on a service not yet registered; module constructor doing I/O or configuration that fails on device; module registered for the wrong platform; version drift between module and host DI registrations.","solutions":["Read the logged error (LogError with 'An error ocurred while initializing the Modules') and the InnerException to identify the failing module.","Wrap or fix the throwing code in the module's Initialize/RegisterTypes; register any missing dependencies before module initialization.","Change the module to InitializationMode.WhenAvailable if it need not initialize at startup.","Test the module in isolation to reproduce and fix the constructor/initialization failure."],"exampleFix":"// before: module Initialize does I/O that throws\npublic void Initialize(IContainerProvider container) => LoadCacheFromDisk();\n// after: guard and defer\npublic void Initialize(IContainerProvider container)\n{\n    if (File.Exists(CachePath)) LoadCacheFromDisk();\n}","handlingStrategy":"try-catch","validationCode":"// Ensure module dependencies resolve before Run\nforeach (var m in catalog.Modules)\n    container.Resolve(m.ModuleType);","typeGuard":null,"tryCatchPattern":"try\n{\n    moduleManager.Run();\n}\ncatch (PrismInitializationException ex)\n{\n    logger.LogError(ex.InnerException, \"Module init failed\");\n}","preventionTips":["Keep module Initialize/RegisterTypes free of I/O and throwing logic.","Register all services a module needs before module initialization runs.","Use InitializationMode.WhenAvailable for non-essential modules."],"tags":["dotnet","maui","prism","modules","initialization"],"backgroundTag":"module-init-failed","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}