{"record":{"id":"5b186f3194812e81","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/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/NavigationManager.cs#L270-L306","documentation":"NavigationManager.Initialize can only be called once. It sets BaseUri and Uri before first use; a second call throws InvalidOperationException because re-initializing would invalidate already-issued URIs and subscribers.","triggerScenarios":"Calling Initialize twice, e.g. during Blazor Server reconnection, middleware that re-initializes, or test setup that reuses an instance.","commonSituations":"Blazor Server reconnection paths that re-init, custom hosting that calls Initialize on every request, or unit tests sharing a NavigationManager instance across cases.","solutions":["Guard the Initialize call so it runs only once per instance.","Create a fresh NavigationManager instance when re-initialization is genuinely needed.","Restructure the lifecycle so initialization happens at startup only."],"exampleFix":"// before\nmanager.Initialize(baseUri, uri);  // called again\n// after\nif (!manager.IsInitialized)\n{\n    manager.Initialize(baseUri, uri);\n}","handlingStrategy":"validation","validationCode":"// Guard Initialize so it runs once per instance.\nif (!_isInitialized)\n{\n    Initialize(baseUri, uri);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call NavigationManager.Initialize exactly once per instance lifecycle.","Create a new instance for tests instead of re-initializing a shared one.","Guard reconnection/middleware paths to skip Initialize if already initialized."],"tags":["blazor","navigation","navigation-manager","initialization"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}