dotnet/aspnetcore · error · InvalidOperationException

{nameof(DynamicComponent)} requires a non-null value for the

Error message

{nameof(DynamicComponent)} requires a non-null value for the parameter {nameof(Type)}.

What it means

Thrown by DynamicComponent.SetParametersAsync when the Type parameter is null after all parameters are processed. DynamicComponent needs to know which component to render; without a Type it cannot proceed. Although Type is marked [EditorRequired], the runtime still enforces non-null explicitly at DynamicComponent.cs:84-87.

Source

Thrown at src/Components/Components/src/DynamicComponent.cs:86

        {
            if (entry.Name.Equals(nameof(Type), StringComparison.OrdinalIgnoreCase))
            {
                Type = (Type)entry.Value;
            }
            else if (entry.Name.Equals(nameof(Parameters), StringComparison.OrdinalIgnoreCase))
            {
                Parameters = (IDictionary<string, object>)entry.Value;
            }
            else
            {
                throw new InvalidOperationException(
                    $"{nameof(DynamicComponent)} does not accept a parameter with the name '{entry.Name}'. To pass parameters to the dynamically-rendered component, use the '{nameof(Parameters)}' parameter.");
            }
        }

        if (Type is null)
        {
            throw new InvalidOperationException($"{nameof(DynamicComponent)} requires a non-null value for the parameter {nameof(Type)}.");
        }

        _renderHandle.Render(_cachedRenderFragment);
        return Task.CompletedTask;
    }

    void Render(RenderTreeBuilder builder)
    {
        builder.OpenComponent(0, Type);

        if (Parameters != null)
        {
            foreach (var entry in Parameters)
            {
                builder.AddComponentParameter(1, entry.Key, entry.Value);
            }
        }

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Always supply a non-null Type: <DynamicComponent Type="@someNonNullType" />.
  2. Guard the DynamicComponent behind an @if(type is not null) so it only renders when Type is known.
  3. Initialize the bound Type property to a concrete default component type.

Example fix

// before
<DynamicComponent Type="@SelectedType" />
@code { private Type? SelectedType; /* null initially */ }

// after
@if (SelectedType is not null)
{
    <DynamicComponent Type="@SelectedType" />
}
Defensive patterns

Strategy: validation

Validate before calling

if (Type is null)
    throw new InvalidOperationException("DynamicComponent requires a non-null Type.");

Type guard

static bool HasValidType(Type? t) => t is not null && typeof(IComponent).IsAssignableFrom(t);

Prevention

When it happens

Trigger: Using <DynamicComponent> without a Type attribute, or binding Type to a variable/expression that evaluates to null (e.g., a Type selected from a list that has no selection).

Common situations: Conditional rendering where the component type hasn't been selected yet; binding Type to a null property; forgetting to set Type in markup.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/7b275ea4c39c85dc. Report an issue: GitHub.