PrismLibrary/Prism · error · InvalidOperationException

The host has not yet been created. The Shell must first be…

Error message

The host has not yet been created. The Shell must first be loaded before the Host is created.

What it means

PrismApplicationBase.Host is a lazy property that returns the IHost built only after the Shell (main window/content) has loaded and OnInitialized has run. Accessing it before that point throws InvalidOperationException. It enforces application lifecycle ordering: modules and host-dependent code must run after shell load.

Solutions

  1. Move Host access into OnInitialized (after the shell is loaded) or later (e.g., module OnInitialized, shell Loaded event).
  2. If needed during startup, resolve the underlying service from Container instead of the Host property, or delay the work until the Shell's Loaded event.
  3. Register types via RegisterTypes (which does not need Host) and resolve Host-dependent objects only after OnInitialized.
  4. Guard access: only read Host once shell load is confirmed (e.g., subscribe to the shell's Loaded event before using Host).

Example fix

// before
protected override void RegisterTypes(IContainerRegistry r)
{
    var host = ((PrismApplicationBase)Application.Current).Host; // too early
}

// after
protected override void OnInitialized()
{
    var host = Host; // Shell is loaded; safe here
    base.OnInitialized();
}
Defensive patterns

Strategy: validation

Validate before calling

// before accessing Host
var app = (PrismApplicationBase)Application.Current;
if (app.Container == null || !shellLoaded) // shellLoaded tracked via shell's Loaded event
    throw new InvalidOperationException("Host not ready; wait for shell load.");

Type guard

static bool IsHostReady(PrismApplicationBase app) =>
    app.Host is not null; // guarded: wraps the throwing property check via try
// safer: use reflection/flag set in OnInitialized to avoid touching Host too early

Try / catch

try
{
    var host = ((PrismApplicationBase)Application.Current).Host;
}
catch (InvalidOperationException ex) when (ex.Message.Contains("host has not yet been created"))
{
    // defer work until OnInitialized / shell Loaded
}

Prevention

When it happens

Trigger: Accessing (PrismApplication.Current as PrismApplicationBase).Host in the App constructor, before base.Initialize, inside RegisterTypes/ConfigureModuleCatalog, in OnInitialized before shell load completes, or from an early service/constructor that runs during startup.

Common situations: Trying to resolve host-based services during App startup; a module accessing Host in its constructor or RegisterTypes rather than OnInitialized/after shell load; WPF/Uno startup code (e.g., XAML converters or static initializers) touching Host too early; moving code from App.xaml.cs startup into RegisterTypes.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Uno/Prism.Uno/PrismApplicationBase.cs:39

        private IHost? _host;
        private IRegionManager? _regionManager;

        protected PrismApplicationBase()
        {
            _containerExtension = CreateContainerExtension();
            ContainerLocator.SetContainerExtension(_containerExtension);
        }

        /// <summary>
        /// The dependency injection container used to resolve objects
        /// </summary>
        public IContainerProvider Container => _containerExtension;

        /// <summary>
        /// Gets the <see cref="IHost" /> built when the Shell is loaded.
        /// </summary>
        public IHost Host => _host ??
            throw new InvalidOperationException("The host has not yet been created. The Shell must first be loaded before the Host is created.");

        /// <summary>
        /// Gets the <see cref="IRegionManager" /> which can be used in the OnInitialized method to Navigation in the Shell once it has loaded
        /// and the <see cref="IHost" /> has been built and all <see cref="IModule" />'s have been loaded by the <see cref="IModuleManager" />
        /// </summary>
        protected IRegionManager RegionManager => _regionManager ??= Container.Resolve<IRegionManager>();

        /// <summary>
        /// Invoked when the application is launched.
        /// </summary>
        /// <param name="args">Event data for the event.</param>
        /// <remarks>If you need to change the behavior here you should override <see cref="Initialize(IApplicationBuilder)"/>.</remarks>
        protected sealed override void OnLaunched(LaunchActivatedEventArgs args)
        {
            base.OnLaunched(args);
            InitializeInternal(this.CreateBuilder(args));
        }

View on GitHub (pinned to 358118cd64)