{"record":{"id":"ca6ea4a5200f0dfa","repo":"dotnet/aspnetcore","slug":"state-already-initialized","errorCode":null,"errorMessage":"State already initialized.","messagePattern":"State already initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/PersistentState/ComponentStatePersistenceManager.cs","lineNumber":78,"sourceCode":"    {\n        await RestoreStateAsync(store, RestoreContext.InitialValue);\n    }\n\n    /// <summary>\n    /// Restores the application state.\n    /// </summary>\n    /// <param name=\"store\"> The <see cref=\"IPersistentComponentStateStore\"/> to restore the application state from.</param>\n    /// <param name=\"context\">The <see cref=\"RestoreContext\"/> that provides additional context for the restoration.</param>\n    /// <returns>A <see cref=\"Task\"/> that will complete when the state has been restored.</returns>\n    public async Task RestoreStateAsync(IPersistentComponentStateStore store, RestoreContext context)\n    {\n        var data = await store.GetPersistedStateAsync();\n\n        if (_stateIsInitialized)\n        {\n            if (context != RestoreContext.ValueUpdate)\n            {\n                throw new InvalidOperationException(\"State already initialized.\");\n            }\n            State.UpdateExistingState(data, context);\n            foreach (var registration in _registeredRestoringCallbacks)\n            {\n                registration.Callback();\n            }\n        }\n        else\n        {\n            State.InitializeExistingState(data, context);\n            _servicesRegistry?.RegisterForPersistence(State);\n            _stateIsInitialized = true;\n        }\n    }\n\n    /// <summary>\n    /// Persists the component application state into the given <see cref=\"IPersistentComponentStateStore\"/>.\n    /// </summary>","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/PersistentState/ComponentStatePersistenceManager.cs#L60-L96","documentation":"Thrown by ComponentStatePersistenceManager.RestoreStateAsync when _stateIsInitialized is already true and the supplied RestoreContext is not ValueUpdate. The manager permits re-entry only for RestoreContext.ValueUpdate (which routes to State.UpdateExistingState); any other context on an already-initialized manager is treated as a misuse.","triggerScenarios":"Calling RestoreStateAsync(store) (default context) a second time on the same ComponentStatePersistenceManager instance; middleware that restores on every request without gating on initialization.","commonSituations":"Custom host/middleware that wires restore per-request without a once-per-lifetime guard; a test that reuses one manager across requests; double-registration of restore middleware in the pipeline.","solutions":["Call the initial RestoreStateAsync exactly once per manager lifetime; gate with your own flag or rely on DI scoped/singleton semantics.","For subsequent in-place updates pass RestoreContext.ValueUpdate explicitly.","If a fresh restore is genuinely needed, use a new ComponentStatePersistenceManager instance (new DI scope / new circuit).","Confirm framework integration isn't already restoring before your code does."],"exampleFix":"// before\nawait manager.RestoreStateAsync(store); // called on every request -> throws 2nd time\n\n// after\nif (!_restored)\n{\n    await manager.RestoreStateAsync(store);\n    _restored = true;\n}\nelse\n{\n    await manager.RestoreStateAsync(store, RestoreContext.ValueUpdate);\n}","handlingStrategy":"validation","validationCode":"private bool _restored;\n\nasync Task RestoreOnceAsync(ComponentStatePersistenceManager manager, IPersistentComponentStateStore store)\n{\n    if (_restored)\n    {\n        // subsequent updates must use ValueUpdate\n        await manager.RestoreStateAsync(store, RestoreContext.ValueUpdate);\n        return;\n    }\n    await manager.RestoreStateAsync(store);\n    _restored = true;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await manager.RestoreStateAsync(store);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"State already initialized.\")\n{\n    // already restored - route through ValueUpdate instead\n    await manager.RestoreStateAsync(store, RestoreContext.ValueUpdate);\n}","preventionTips":["Gate the initial restore with a flag so it runs once per manager lifetime.","Match DI lifetime to restore lifetime (scoped per circuit).","Use RestoreContext.ValueUpdate for any subsequent restore on the same manager.","Audit middleware for duplicate restore invocations."],"tags":["blazor","component-state","persistence","restore","lifecycle"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}