{"record":{"id":"c5c6e470ad93385f","repo":"PrismLibrary/Prism","slug":"the-host-has-not-yet-been-created-the-shell-must-first-be","errorCode":null,"errorMessage":"The host has not yet been created. The Shell must first be loaded before the Host is created.","messagePattern":"The host has not yet been created\\. The Shell must first be loaded before the Host is created\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Uno/Prism.Uno/PrismApplicationBase.cs","lineNumber":39,"sourceCode":"        private IHost? _host;\n        private IRegionManager? _regionManager;\n\n        protected PrismApplicationBase()\n        {\n            _containerExtension = CreateContainerExtension();\n            ContainerLocator.SetContainerExtension(_containerExtension);\n        }\n\n        /// <summary>\n        /// The dependency injection container used to resolve objects\n        /// </summary>\n        public IContainerProvider Container => _containerExtension;\n\n        /// <summary>\n        /// Gets the <see cref=\"IHost\" /> built when the Shell is loaded.\n        /// </summary>\n        public IHost Host => _host ??\n            throw new InvalidOperationException(\"The host has not yet been created. The Shell must first be loaded before the Host is created.\");\n\n        /// <summary>\n        /// Gets the <see cref=\"IRegionManager\" /> which can be used in the OnInitialized method to Navigation in the Shell once it has loaded\n        /// and the <see cref=\"IHost\" /> has been built and all <see cref=\"IModule\" />'s have been loaded by the <see cref=\"IModuleManager\" />\n        /// </summary>\n        protected IRegionManager RegionManager => _regionManager ??= Container.Resolve<IRegionManager>();\n\n        /// <summary>\n        /// Invoked when the application is launched.\n        /// </summary>\n        /// <param name=\"args\">Event data for the event.</param>\n        /// <remarks>If you need to change the behavior here you should override <see cref=\"Initialize(IApplicationBuilder)\"/>.</remarks>\n        protected sealed override void OnLaunched(LaunchActivatedEventArgs args)\n        {\n            base.OnLaunched(args);\n            InitializeInternal(this.CreateBuilder(args));\n        }\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Uno/Prism.Uno/PrismApplicationBase.cs#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Move Host access into OnInitialized (after the shell is loaded) or later (e.g., module OnInitialized, shell Loaded event).","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.","Register types via RegisterTypes (which does not need Host) and resolve Host-dependent objects only after OnInitialized.","Guard access: only read Host once shell load is confirmed (e.g., subscribe to the shell's Loaded event before using Host)."],"exampleFix":"// before\nprotected override void RegisterTypes(IContainerRegistry r)\n{\n    var host = ((PrismApplicationBase)Application.Current).Host; // too early\n}\n\n// after\nprotected override void OnInitialized()\n{\n    var host = Host; // Shell is loaded; safe here\n    base.OnInitialized();\n}","handlingStrategy":"validation","validationCode":"// before accessing Host\nvar app = (PrismApplicationBase)Application.Current;\nif (app.Container == null || !shellLoaded) // shellLoaded tracked via shell's Loaded event\n    throw new InvalidOperationException(\"Host not ready; wait for shell load.\");","typeGuard":"static bool IsHostReady(PrismApplicationBase app) =>\n    app.Host is not null; // guarded: wraps the throwing property check via try\n// safer: use reflection/flag set in OnInitialized to avoid touching Host too early","tryCatchPattern":"try\n{\n    var host = ((PrismApplicationBase)Application.Current).Host;\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"host has not yet been created\"))\n{\n    // defer work until OnInitialized / shell Loaded\n}","preventionTips":["Access Host only from OnInitialized or later lifecycle points","Use Container.Resolve for services needed during RegisterTypes","Defer host-dependent startup work to the shell's Loaded event","Never touch PrismApplicationBase.Host in App/module constructors"],"tags":["prism","uno","lifecycle","host","invalid-state"],"backgroundTag":"invalid-state-transition","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"}