dotnet/aspnetcore · error · InvalidOperationException

' ' already initialized.

Error message

'{GetType().Name}' already initialized.

What it means

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.

Solutions

  1. Guard the Initialize call so it runs only once per instance.
  2. Create a fresh NavigationManager instance when re-initialization is genuinely needed.
  3. Restructure the lifecycle so initialization happens at startup only.

Example fix

// before
manager.Initialize(baseUri, uri);  // called again
// after
if (!manager.IsInitialized)
{
    manager.Initialize(baseUri, uri);
}
Defensive patterns

Strategy: validation

Validate before calling

// Guard Initialize so it runs once per instance.
if (!_isInitialized)
{
    Initialize(baseUri, uri);
}

Prevention

When it happens

Trigger: Calling Initialize twice, e.g. during Blazor Server reconnection, middleware that re-initializes, or test setup that reuses an instance.

Common situations: Blazor Server reconnection paths that re-init, custom hosting that calls Initialize on every request, or unit tests sharing a NavigationManager instance across cases.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/5b186f3194812e81. Report an issue: GitHub.

Appendix: source

Thrown at src/Components/Components/src/NavigationManager.cs:288

        else
        {
            _notFound.Invoke(this, new NotFoundEventArgs());
        }
    }

    /// <summary>
    /// Called to initialize BaseURI and current URI before these values are used for the first time.
    /// Override <see cref="EnsureInitialized" /> and call this method to dynamically calculate these values.
    /// </summary>
    protected void Initialize(string baseUri, string uri)
    {
        // Make sure it's possible/safe to call this method from constructors of derived classes.
        ArgumentNullException.ThrowIfNull(uri);
        ArgumentNullException.ThrowIfNull(baseUri);

        if (_isInitialized)
        {
            throw new InvalidOperationException($"'{GetType().Name}' already initialized.");
        }

        _isInitialized = true;

        // Setting BaseUri before Uri so they get validated.
        BaseUri = baseUri;
        Uri = uri;
    }

    /// <summary>
    /// Allows derived classes to lazily self-initialize. Implementations that support lazy-initialization should override
    /// this method and call <see cref="Initialize(string, string)" />.
    /// </summary>
    protected virtual void EnsureInitialized()
    {
    }

    /// <summary>

View on GitHub (pinned to 3600ca084e)