{"record":{"id":"995754edd316b57b","repo":"dotnet/aspnetcore","slug":"there-is-no-enclosing-component-frame","errorCode":null,"errorMessage":"There is no enclosing component frame.","messagePattern":"There is no enclosing component frame\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/Rendering/RenderTreeBuilder.cs","lineNumber":651,"sourceCode":"    /// <param name=\"renderMode\">The <see cref=\"IComponentRenderMode\"/>.</param>\n    public void AddComponentRenderMode(IComponentRenderMode? renderMode)\n    {\n        if (renderMode is null)\n        {\n            return;\n        }\n\n        // Note that a ComponentRenderMode frame is technically a child of the Component frame to which it applies,\n        // hence the terminology of \"adding\" it rather than \"setting\" it. For performance reasons, the diffing system\n        // will only look for ComponentRenderMode frames:\n        // [a] when the HasCallerSpecifiedRenderMode flag is set on the Component frame\n        // [b] up until the first child that is *not* a ComponentRenderMode frame or any other header frame type\n        //     that we may define in the future\n\n        var parentFrameIndex = GetCurrentParentFrameIndex();\n        if (!parentFrameIndex.HasValue)\n        {\n            throw new InvalidOperationException(\"There is no enclosing component frame.\");\n        }\n\n        var parentFrameIndexValue = parentFrameIndex.Value;\n        ref var parentFrame = ref _entries.Buffer[parentFrameIndexValue];\n        if (parentFrame.FrameTypeField != RenderTreeFrameType.Component)\n        {\n            throw new InvalidOperationException($\"The enclosing frame is not of the required type '{nameof(RenderTreeFrameType.Component)}'.\");\n        }\n\n        parentFrame.ComponentFrameFlagsField |= ComponentFrameFlags.HasCallerSpecifiedRenderMode;\n\n        _entries.AppendComponentRenderMode(renderMode);\n        _lastNonAttributeFrameType = RenderTreeFrameType.ComponentRenderMode;\n    }\n\n    /// <summary>\n    /// Assigns a name to an event in the enclosing element.\n    /// </summary>","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/Rendering/RenderTreeBuilder.cs#L633-L669","documentation":"Thrown by RenderTreeBuilder.AddComponentRenderMode (emitted by @rendermode on a child component or by an explicit AddComponentRenderMode call) when GetCurrentParentFrameIndex() is null — there is no enclosing frame on the stack at all. A render-mode frame is attached to the component whose rendering mode it sets, so it cannot stand alone at the root.","triggerScenarios":"Calling builder.AddComponentRenderMode(mode) at the top level of a RenderFragment with no OpenComponent preceding it; a @rendermode applied where no component was opened.","commonSituations":"Manually building a tree and calling AddComponentRenderMode before/without OpenComponent; refactoring that removed an OpenComponent but left the render-mode call; misuse in low-level library helpers.","solutions":["Call AddComponentRenderMode only after OpenComponent(...) and before CloseComponent().","Confirm the render-mode call is paired 1:1 with an OpenComponent in the same scope.","Delete orphaned AddComponentRenderMode calls whose component was removed."],"exampleFix":"// before\nbuilder.AddComponentRenderMode(RenderMode.InteractiveServer);\nbuilder.OpenComponent<MyComp>(0);\nbuilder.CloseComponent();\n\n// after\nbuilder.OpenComponent<MyComp>(0);\nbuilder.AddComponentRenderMode(RenderMode.InteractiveServer);\nbuilder.CloseComponent();","handlingStrategy":"validation","validationCode":"// Ensure AddComponentRenderMode runs only when a component is the current scope.\nint _compDepth = 0;\nvoid Render(RenderTreeBuilder b) {\n    b.OpenComponent<MyComp>(0); _compDepth++;\n    if (_compDepth > 0 && MyRenderMode is not null) b.AddComponentRenderMode(MyRenderMode);\n    _compDepth--; b.CloseComponent();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call AddComponentRenderMode only directly after OpenComponent.","Prefer the .razor @rendermode directive over manual AddComponentRenderMode calls.","Guard AddComponentRenderMode against null mode (the API no-ops on null, but the call site should still be inside a component scope)."],"tags":["blazor","rendering","rendertree","rendermode","interactive"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}