{"record":{"id":"0a068664a7ccfb0f","repo":"dotnet/aspnetcore","slug":"persistentcomponentstate-already-initialized","errorCode":null,"errorMessage":"PersistentComponentState already initialized.","messagePattern":"PersistentComponentState already initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/PersistentComponentState.cs","lineNumber":40,"sourceCode":"    internal PersistentComponentState(\n        IDictionary<string, byte[]> currentState,\n        List<PersistComponentStateRegistration> pauseCallbacks,\n        List<RestoreComponentStateRegistration> restoringCallbacks)\n    {\n        _currentState = currentState;\n        _registeredCallbacks = pauseCallbacks;\n        _registeredRestoringCallbacks = restoringCallbacks;\n    }\n\n    internal bool PersistingState { get; set; }\n\n    internal RestoreContext CurrentContext { get; private set; } = RestoreContext.InitialValue;\n\n    internal void InitializeExistingState(IDictionary<string, byte[]> existingState, RestoreContext context)\n    {\n        if (_existingState != null)\n        {\n            throw new InvalidOperationException(\"PersistentComponentState already initialized.\");\n        }\n        _existingState = existingState ?? throw new ArgumentNullException(nameof(existingState));\n        CurrentContext = context;\n    }\n\n    /// <summary>\n    /// Register a callback to persist the component state when the application is about to be paused.\n    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.\n    /// </summary>\n    /// <param name=\"callback\">The callback to invoke when the application is being paused.</param>\n    /// <returns>A subscription that can be used to unregister the callback when disposed.</returns>\n    public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback)\n        => RegisterOnPersisting(callback, null);\n\n    /// <summary>\n    /// Register a callback to persist the component state when the application is about to be paused.\n    /// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes.\n    /// </summary>","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/PersistentComponentState.cs#L22-L58","documentation":"Thrown by PersistentComponentState.InitializeExistingState when called more than once. PersistentComponentState holds a single restoration table; re-initializing would either discard previously registered state or duplicate it. The framework calls this exactly once per render cycle; a second call indicates double initialization.","triggerScenarios":"The framework host (prerendering host, WebAssembly start, or a custom host) calls InitializeExistingState twice in one lifetime. In user code this is reached only if you manually drive PersistentComponentState in a custom hosting scenario or in a test that re-uses the same instance across renders.","commonSituations":"A test fixture that constructs PersistentComponentState once but initializes it per-test without resetting; a custom prerendering pipeline that runs twice; double-calling during a misconfigured prerender + interactive handoff.","solutions":["Do not call InitializeExistingState yourself - the framework owns its lifecycle.","In tests, create a fresh PersistentComponentState per test rather than re-initializing one instance.","If you build a custom host, ensure the prerender/restore path runs exactly once per circuit or page render.","Check that middleware or an interceptor is not re-running the persistence pipeline."],"exampleFix":"// before: reused state instance re-initialized each test\n_state.InitializeExistingState(data, context); // throws on second test\n\n// after: new instance per test, or clear first\n_state = new PersistentComponentState(current, callbacks, restoringCallbacks);\n_state.InitializeExistingState(data, context);","handlingStrategy":"validation","validationCode":"// Framework owns this; in tests, build a fresh instance per test.\nif (_stateAlreadyInitialized) throw new InvalidOperationException(\"PersistentComponentState already initialized.\");","typeGuard":null,"tryCatchPattern":"try { state.InitializeExistingState(data, context); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already initialized\"))\n{ /* create a new PersistentComponentState instead */ }","preventionTips":["Never call InitializeExistingState from application code; the framework owns it.","In tests, create a new PersistentComponentState per test case.","In a custom host, run the restore/persist pipeline exactly once per circuit or page render."],"tags":["blazor","state","persistence","lifecycle"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}