dotnet/aspnetcore · error · InvalidOperationException

PersistentComponentState already initialized.

Error message

PersistentComponentState already initialized.

What it means

PersistentComponentState.InitializeExistingState throws InvalidOperationException if called twice, because _existingState is already set. The framework initializes persisted state once per prerender/restore cycle; a second call indicates duplicate setup logic.

Source

Thrown at src/Components/Components/src/PersistentComponentState.cs:40

    internal PersistentComponentState(
        IDictionary<string, byte[]> currentState,
        List<PersistComponentStateRegistration> pauseCallbacks,
        List<RestoreComponentStateRegistration> restoringCallbacks)
    {
        _currentState = currentState;
        _registeredCallbacks = pauseCallbacks;
        _registeredRestoringCallbacks = restoringCallbacks;
    }

    internal bool PersistingState { get; set; }

    internal RestoreContext CurrentContext { get; private set; } = RestoreContext.InitialValue;

    internal void InitializeExistingState(IDictionary<string, byte[]> existingState, RestoreContext context)
    {
        if (_existingState != null)
        {
            throw new InvalidOperationException("PersistentComponentState already initialized.");
        }
        _existingState = existingState ?? throw new ArgumentNullException(nameof(existingState));
        CurrentContext = context;
    }

    /// <summary>
    /// Register a callback to persist the component state when the application is about to be paused.
    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.
    /// </summary>
    /// <param name="callback">The callback to invoke when the application is being paused.</param>
    /// <returns>A subscription that can be used to unregister the callback when disposed.</returns>
    public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback)
        => RegisterOnPersisting(callback, null);

    /// <summary>
    /// Register a callback to persist the component state when the application is about to be paused.
    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.
    /// </summary>

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Call InitializeExistingState exactly once per PersistentComponentState lifetime (per request).
  2. Register PersistentComponentState as scoped, not singleton.
  3. Guard host code against double initialization (check before calling).

Example fix

// before
state.InitializeExistingState(data, ctx);
state.InitializeExistingState(data, ctx); // duplicate -> throws

// after
state.InitializeExistingState(data, ctx); // single call per request scope
Defensive patterns

Strategy: validation

Validate before calling

// Host code: call once per scoped instance
if (_stateInitialized) return;
state.InitializeExistingState(data, ctx);
_stateInitialized = true;

Try / catch

try { state.InitializeExistingState(data, ctx); }
catch (InvalidOperationException ex) when (ex.Message.Contains("already initialized"))
{ logger.LogDebug("PersistentComponentState already initialized"); }

Prevention

When it happens

Trigger: Custom host calling InitializeExistingState twice; middleware or a custom renderer re-running the persist/restore pipeline on the same instance; DI mis-scoping PersistentComponentState as a singleton that gets reused across requests.

Common situations: Custom prerendering setup that invokes initialization in more than one place; tests that reuse the instance across render cycles; framework upgrade changing the initialization call frequency.

Related errors


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