{"record":{"id":"ff2795cb74c239ab","repo":"dotnet/aspnetcore","slug":"render-mode-already-set","errorCode":null,"errorMessage":"Render mode already set.","messagePattern":"Render mode already set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/PersistentState/ComponentStatePersistenceManager.cs","lineNumber":167,"sourceCode":"            await store.PersistStateAsync(_currentState);\n            return true;\n        }\n    }\n\n    /// <summary>\n    /// Initializes the render mode for state persisted by the platform.\n    /// </summary>\n    /// <param name=\"renderMode\">The render mode to use for state persisted by the platform.</param>\n    /// <exception cref=\"InvalidOperationException\">when the render mode is already set.</exception>\n    public void SetPlatformRenderMode(IComponentRenderMode renderMode)\n    {\n        if (_servicesRegistry == null)\n        {\n            return;\n        }\n        else if (_servicesRegistry?.RenderMode != null)\n        {\n            throw new InvalidOperationException(\"Render mode already set.\");\n        }\n\n        _servicesRegistry!.RenderMode = renderMode;\n    }\n\n    private void InferRenderModes(Renderer renderer)\n    {\n        // We are iterating backwards to allow the callbacks to remove themselves from the list.\n        // Otherwise, we would have to make a copy of the list to avoid running into situations\n        // where we don't run all the callbacks because the count of the list changed while we\n        // were iterating over it.\n        // It is not allowed to register a callback while we are persisting the state, so we don't\n        // need to worry about new callbacks being added to the list.\n        for (var i = _registeredCallbacks.Count - 1; i >= 0; i--)\n        {\n            var registration = _registeredCallbacks[i];\n            if (registration.RenderMode != null)\n            {","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/PersistentState/ComponentStatePersistenceManager.cs#L149-L185","documentation":"Thrown by ComponentStatePersistenceManager.SetPlatformRenderMode when _servicesRegistry.RenderMode is already non-null. The platform render mode is a one-time configuration applied to the persistent-services registry and cannot be changed after it is set.","triggerScenarios":"Calling SetPlatformRenderMode more than once with any render mode argument.","commonSituations":"Custom host that configures render mode per request; integration code that sets it both at startup and again per-endpoint; a test fixture that re-initializes without a fresh manager.","solutions":["Call SetPlatformRenderMode exactly once during startup/configuration.","Guard the call so it only runs on first configuration.","If you must change render mode, use a new ComponentStatePersistenceManager instance.","When using the default framework integration, let it set the platform render mode instead of calling this manually."],"exampleFix":"// before\napp.Use(async (ctx, next) => {\n    manager.SetPlatformRenderMode(InteractiveServer); // runs each request -> throws\n    await next();\n});\n\n// after: set once at startup\nmanager.SetPlatformRenderMode(InteractiveServer);","handlingStrategy":"validation","validationCode":"private bool _renderModeSet;\n\nvoid SetPlatformRenderModeOnce(ComponentStatePersistenceManager manager, IComponentRenderMode mode)\n{\n    if (_renderModeSet) return;\n    manager.SetPlatformRenderMode(mode);\n    _renderModeSet = true;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    manager.SetPlatformRenderMode(mode);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Render mode already set.\")\n{\n    // already configured - safe to ignore in idempotent setup paths\n}","preventionTips":["Call SetPlatformRenderMode only during startup, not per request.","Guard the call so it runs once.","Prefer the framework's own integration over manual calls.","Use a fresh manager if the render mode must change."],"tags":["blazor","component-state","render-mode","configuration"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}