dotnet/aspnetcore · error · InvalidOperationException

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

Error message

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

What it means

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.

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 294cab2f9b)

Solutions

  1. Ensure Initialize is called exactly once per instance; guard with your own flag or rely on the framework's single call site.
  2. Do not call Initialize from both a constructor and EnsureInitialized.
  3. Register NavigationManager as scoped rather than singleton if state must reset per request.

Example fix

// before
protected override void EnsureInitialized()
{
    Initialize(_baseUri, _uri); // called every access after init -> throws
}

// after
protected override void EnsureInitialized()
{
    if (!_isInitialized) { Initialize(_baseUri, _uri); }
}
Defensive patterns

Strategy: validation

Validate before calling

// Cannot read _isInitialized externally; guard your own Initialize calls
private bool _didInit;
public void InitOnce(string baseUri, string uri)
{
    if (_didInit) return;
    Initialize(baseUri, uri);
    _didInit = true;
}

Try / catch

try { Initialize(baseUri, uri); }
catch (InvalidOperationException ex) when (ex.Message.Contains("already initialized"))
{ logger.LogDebug("NavigationManager already initialized, skipping"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/8ec2d9500a099ab6. Report an issue: GitHub.