{"record":{"id":"6e86fa3e070be706","repo":"PrismLibrary/Prism","slug":"an-error-was-encountered-while-invoking-the-oninitialized","errorCode":null,"errorMessage":"An error was encountered while invoking the OnInitialized Delegates","messagePattern":"An error was encountered while invoking the OnInitialized Delegates","errorType":"exception","errorClass":"PrismInitializationException","httpStatus":null,"severity":"critical","filePath":"src/Maui/Prism.Maui/PrismAppBuilder.cs","lineNumber":252,"sourceCode":"        }\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);\n            }\n        });\n\n        if (errors.Count == 1)\n        {\n            throw new PrismInitializationException(\"An error was encountered while invoking the OnInitialized Delegates\", errors[0]);\n        }\n        else if (errors.Count > 1)\n        {\n            throw new AggregateException(\"One or more errors were encountered while executing the OnInitialized Delegates\", [.. errors]);\n        }\n\n        var navRegistry = _container.Resolve<INavigationRegistry>();\n        if (!navRegistry.IsRegistered(nameof(NavigationPage)))\n        {\n            var registry = _container as IContainerRegistry;\n            registry\n                .Register<PrismNavigationPage>(() => new PrismNavigationPage())\n                .RegisterInstance(new ViewRegistration\n                {\n                    Name = nameof(NavigationPage),\n                    View = typeof(PrismNavigationPage),\n                    Type = ViewType.Page\n                });","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/PrismAppBuilder.cs#L234-L270","documentation":"Prism executes the OnInitialized delegates supplied via UsePrism(b => b.OnInitialized(...)) during app startup and collects exceptions. When exactly one delegate throws, it is rethrown as a PrismInitializationException with this message, preserving the cause as InnerException.","triggerScenarios":"A single OnInitialized callback registered on PrismAppBuilder throws — e.g. resolving a service that fails, calling a navigation or configuration API that throws at startup.","commonSituations":"Startup code resolving unregistered services; throwing initial navigation; license/config checks in OnInitialized failing; exceptions in third-party init called from the delegate.","solutions":["Inspect InnerException to see which delegate and what exception occurred.","Fix the throwing code in the OnInitialized delegate (register missing services, guard failing calls).","Split large delegates so failures point to specific lambdas.","Move non-essential startup work behind lazy/async execution instead of the OnInitialized delegate."],"exampleFix":"// before\n.OnInitialized(container => { var svc = container.Resolve<IReportSync>(); svc.SyncNow(); }) // SyncNow throws\n// after\n.OnInitialized(container => { try { container.Resolve<IReportSync>().SyncNow(); } catch (Exception ex) { Log(ex); } })","handlingStrategy":"try-catch","validationCode":"// Check services needed by OnInitialized delegates are registered\nif (!container.IsRegistered<IConfig>()) throw new InvalidOperationException(\"IConfig not registered\");","typeGuard":null,"tryCatchPattern":"try\n{\n    RunStartupDelegates();\n}\ncatch (PrismInitializationException ex)\n{\n    logger.LogError(ex.InnerException, \"OnInitialized delegate threw\");\n}","preventionTips":["Keep OnInitialized delegates small and defensive.","Log inside each delegate so failures are attributable.","Defer risky work until after the first navigation completes."],"tags":["dotnet","maui","prism","startup","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"}