dotnet/aspnetcore · error · InvalidOperationException
There is no enclosing component frame.
Error message
There is no enclosing component frame.
What it means
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.
Source
Thrown at src/Components/Components/src/Rendering/RenderTreeBuilder.cs:651
/// <param name="renderMode">The <see cref="IComponentRenderMode"/>.</param>
public void AddComponentRenderMode(IComponentRenderMode? renderMode)
{
if (renderMode is null)
{
return;
}
// Note that a ComponentRenderMode frame is technically a child of the Component frame to which it applies,
// hence the terminology of "adding" it rather than "setting" it. For performance reasons, the diffing system
// will only look for ComponentRenderMode frames:
// [a] when the HasCallerSpecifiedRenderMode flag is set on the Component frame
// [b] up until the first child that is *not* a ComponentRenderMode frame or any other header frame type
// that we may define in the future
var parentFrameIndex = GetCurrentParentFrameIndex();
if (!parentFrameIndex.HasValue)
{
throw new InvalidOperationException("There is no enclosing component frame.");
}
var parentFrameIndexValue = parentFrameIndex.Value;
ref var parentFrame = ref _entries.Buffer[parentFrameIndexValue];
if (parentFrame.FrameTypeField != RenderTreeFrameType.Component)
{
throw new InvalidOperationException($"The enclosing frame is not of the required type '{nameof(RenderTreeFrameType.Component)}'.");
}
parentFrame.ComponentFrameFlagsField |= ComponentFrameFlags.HasCallerSpecifiedRenderMode;
_entries.AppendComponentRenderMode(renderMode);
_lastNonAttributeFrameType = RenderTreeFrameType.ComponentRenderMode;
}
/// <summary>
/// Assigns a name to an event in the enclosing element.
/// </summary>View on GitHub (pinned to 294cab2f9b)
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.
Example fix
// before builder.AddComponentRenderMode(RenderMode.InteractiveServer); builder.OpenComponent<MyComp>(0); builder.CloseComponent(); // after builder.OpenComponent<MyComp>(0); builder.AddComponentRenderMode(RenderMode.InteractiveServer); builder.CloseComponent();
Defensive patterns
Strategy: validation
Validate before calling
// Ensure AddComponentRenderMode runs only when a component is the current scope.
int _compDepth = 0;
void Render(RenderTreeBuilder b) {
b.OpenComponent<MyComp>(0); _compDepth++;
if (_compDepth > 0 && MyRenderMode is not null) b.AddComponentRenderMode(MyRenderMode);
_compDepth--; b.CloseComponent();
} Prevention
- 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).
When it happens
Trigger: Calling builder.AddComponentRenderMode(mode) at the top level of a RenderFragment with no OpenComponent preceding it; a @rendermode applied where no component was opened.
Common situations: 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.
Related errors
- The enclosing frame is not of the required type 'Component'.
- Element reference captures may only be added as children of
- Component reference captures may only be added as children o
- Named events may only be added as children of frames of type
- Attributes may only be added immediately after frames of typ
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/995754edd316b57b.
Report an issue: GitHub.