PrismLibrary/Prism · critical · PrismInitializationException

An error was encountered while invoking the OnInitialized…

Error message

An error was encountered while invoking the OnInitialized Delegates

What it means

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.

Solutions

  1. Inspect InnerException to see which delegate and what exception occurred.
  2. Fix the throwing code in the OnInitialized delegate (register missing services, guard failing calls).
  3. Split large delegates so failures point to specific lambdas.
  4. Move non-essential startup work behind lazy/async execution instead of the OnInitialized delegate.

Example fix

// before
.OnInitialized(container => { var svc = container.Resolve<IReportSync>(); svc.SyncNow(); }) // SyncNow throws
// after
.OnInitialized(container => { try { container.Resolve<IReportSync>().SyncNow(); } catch (Exception ex) { Log(ex); } })
Defensive patterns

Strategy: try-catch

Validate before calling

// Check services needed by OnInitialized delegates are registered
if (!container.IsRegistered<IConfig>()) throw new InvalidOperationException("IConfig not registered");

Try / catch

try
{
    RunStartupDelegates();
}
catch (PrismInitializationException ex)
{
    logger.LogError(ex.InnerException, "OnInitialized delegate threw");
}

Prevention

When it happens

Trigger: 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.

Common situations: Startup code resolving unregistered services; throwing initial navigation; license/config checks in OnInitialized failing; exceptions in third-party init called from the delegate.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/6e86fa3e070be706. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/PrismAppBuilder.cs:252

        }

        // Phase 3: Run OnInitialized delegates (module services are now available)
        _initializations.ForEach(action =>
        {
            try
            {
                action(_container);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error executing Initialization Delegate.");
                errors.Add(ex);
            }
        });

        if (errors.Count == 1)
        {
            throw new PrismInitializationException("An error was encountered while invoking the OnInitialized Delegates", errors[0]);
        }
        else if (errors.Count > 1)
        {
            throw new AggregateException("One or more errors were encountered while executing the OnInitialized Delegates", [.. errors]);
        }

        var navRegistry = _container.Resolve<INavigationRegistry>();
        if (!navRegistry.IsRegistered(nameof(NavigationPage)))
        {
            var registry = _container as IContainerRegistry;
            registry
                .Register<PrismNavigationPage>(() => new PrismNavigationPage())
                .RegisterInstance(new ViewRegistration
                {
                    Name = nameof(NavigationPage),
                    View = typeof(PrismNavigationPage),
                    Type = ViewType.Page
                });

View on GitHub (pinned to 358118cd64)