{"record":{"id":"c576057688bd5444","repo":"dotnet/aspnetcore","slug":"gettype-name-has-not-been-initialized","errorCode":null,"errorMessage":"' {GetType().Name}' has not been initialized.","messagePattern":"' (.+?)' has not been initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/NavigationManager.cs","lineNumber":611,"sourceCode":"    {\n        AssertInitialized();\n\n        if (_locationChangingHandlers.Remove(locationChangingHandler) && _locationChangingHandlers.Count == 0)\n        {\n            SetNavigationLockState(false);\n        }\n    }\n\n    private void AssertInitialized()\n    {\n        if (!_isInitialized)\n        {\n            EnsureInitialized();\n        }\n\n        if (!_isInitialized)\n        {\n            throw new InvalidOperationException($\"'{GetType().Name}' has not been initialized.\");\n        }\n    }\n\n    private static bool TryGetLengthOfBaseUriPrefix(Uri baseUri, string uri, out int length)\n    {\n        if (uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal))\n        {\n            // The absolute URI must be of the form \"{baseUri}something\" (where\n            // baseUri ends with a slash), and from that we return \"something\"\n            length = baseUri.OriginalString.Length;\n            return true;\n        }\n\n        var pathEndIndex = uri.AsSpan().IndexOfAny('#', '?');\n        var uriPathOnly = pathEndIndex < 0 ? uri : uri.AsSpan(0, pathEndIndex);\n        if (baseUri.OriginalString.EndsWith('/') && uriPathOnly.Equals(baseUri.OriginalString.AsSpan(0, baseUri.OriginalString.Length - 1), StringComparison.Ordinal))\n        {\n            // Special case: for the base URI \"/something/\", if you're at","sourceCodeStart":593,"sourceCodeEnd":629,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/NavigationManager.cs#L593-L629","documentation":"AssertInitialized throws InvalidOperationException when, after calling EnsureInitialized, _isInitialized is still false. This means the derived NavigationManager never called Initialize, so BaseUri/Uri are not set and any operation requiring them is unsafe.","triggerScenarios":"Using a custom NavigationManager whose EnsureInitialized override does not call Initialize; accessing Uri/BaseUri/NavigateTo/LocationChanged before the host has initialized the manager; DI resolving NavigationManager too early in a non-Blazor host.","commonSituations":"Console/non-Blazor host wiring missing the Initialize call; component rendering attempted before the renderer set up NavigationManager; test harness that constructs NavigationManager but forgets Initialize.","solutions":["In your NavigationManager subclass, override EnsureInitialized (or the constructor) and call Initialize(baseUri, uri) once.","Ensure the host (renderer/server) initializes NavigationManager before rendering components that consume it.","In tests, call Initialize on the stub before exercising navigation code."],"exampleFix":"// before\npublic class TestNavManager : NavigationManager { /* Initialize never called */ }\n\n// after\npublic class TestNavManager : NavigationManager\n{\n    public TestNavManager() => Initialize(\"http://localhost/\", \"http://localhost/\");\n}","handlingStrategy":"validation","validationCode":"// Cannot read _isInitialized; ensure Initialize is called before use\npublic void EnsureReady()\n{\n    // Trigger AssertInitialized's lazy path or call Initialize once:\n    try { _ = BaseUri; }\n    catch (InvalidOperationException) { Initialize(_defaultBase, _defaultUri); }\n}","typeGuard":null,"tryCatchPattern":"try { var uri = navManager.Uri; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not been initialized\"))\n{ logger.LogError(ex, \"NavigationManager used before Initialize\"); throw; }","preventionTips":["Always call Initialize in custom NavigationManager constructors or EnsureInitialized.","Ensure the host sets up NavigationManager before rendering components.","Initialize stub managers in tests before exercising navigation code."],"tags":["blazor","navigation","initialization","lifecycle","di"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}