{"record":{"id":"8ec2d9500a099ab6","repo":"dotnet/aspnetcore","slug":"gettype-name-already-initialized","errorCode":null,"errorMessage":"' {GetType().Name}' already initialized.","messagePattern":"' (.+?)' already initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/NavigationManager.cs","lineNumber":288,"sourceCode":"        else\n        {\n            _notFound.Invoke(this, new NotFoundEventArgs());\n        }\n    }\n\n    /// <summary>\n    /// Called to initialize BaseURI and current URI before these values are used for the first time.\n    /// Override <see cref=\"EnsureInitialized\" /> and call this method to dynamically calculate these values.\n    /// </summary>\n    protected void Initialize(string baseUri, string uri)\n    {\n        // Make sure it's possible/safe to call this method from constructors of derived classes.\n        ArgumentNullException.ThrowIfNull(uri);\n        ArgumentNullException.ThrowIfNull(baseUri);\n\n        if (_isInitialized)\n        {\n            throw new InvalidOperationException($\"'{GetType().Name}' already initialized.\");\n        }\n\n        _isInitialized = true;\n\n        // Setting BaseUri before Uri so they get validated.\n        BaseUri = baseUri;\n        Uri = uri;\n    }\n\n    /// <summary>\n    /// Allows derived classes to lazily self-initialize. Implementations that support lazy-initialization should override\n    /// this method and call <see cref=\"Initialize(string, string)\" />.\n    /// </summary>\n    protected virtual void EnsureInitialized()\n    {\n    }\n\n    /// <summary>","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/NavigationManager.cs#L270-L306","documentation":"NavigationManager.Initialize throws InvalidOperationException if called more than once, since _isInitialized guards re-entry. Initialize sets the base URI and current URI and is meant to run exactly once per instance lifecycle.","triggerScenarios":"Calling Initialize(baseUri, uri) twice on the same NavigationManager instance; a derived constructor calling Initialize plus EnsureInitialized override also calling it; re-initializing a singleton-scoped NavigationManager across requests.","commonSituations":"Custom NavigationManager where EnsureInitialized is overridden and also calls Initialize; DI misconfiguration registering a singleton that gets re-initialized; test setup calling Initialize before each test on the same instance.","solutions":["Ensure Initialize is called exactly once per instance; guard with your own flag or rely on the framework's single call site.","Do not call Initialize from both a constructor and EnsureInitialized.","Register NavigationManager as scoped rather than singleton if state must reset per request."],"exampleFix":"// before\nprotected override void EnsureInitialized()\n{\n    Initialize(_baseUri, _uri); // called every access after init -> throws\n}\n\n// after\nprotected override void EnsureInitialized()\n{\n    if (!_isInitialized) { Initialize(_baseUri, _uri); }\n}","handlingStrategy":"validation","validationCode":"// Cannot read _isInitialized externally; guard your own Initialize calls\nprivate bool _didInit;\npublic void InitOnce(string baseUri, string uri)\n{\n    if (_didInit) return;\n    Initialize(baseUri, uri);\n    _didInit = true;\n}","typeGuard":null,"tryCatchPattern":"try { Initialize(baseUri, uri); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already initialized\"))\n{ logger.LogDebug(\"NavigationManager already initialized, skipping\"); }","preventionTips":["Call Initialize exactly once, ideally from the host/renderer setup.","Avoid overriding EnsureInitialized to call Initialize unless you guard with a flag.","Prefer scoped registration for per-request NavigationManager instances."],"tags":["blazor","navigation","lifecycle","initialization","di"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}