dotnet/aspnetcore · error · InvalidOperationException

The component activator returned a null value for a componen

Error message

The component activator returned a null value for a component of type {componentType.FullName}.

What it means

Thrown by ComponentFactory.InstantiateComponent when a custom IComponentActivator.CreateInstance returns null. The default activator never returns null, so this only happens with a developer-supplied IComponentActivator registered in DI. The null check is at ComponentFactory.cs:78-82.

Source

Thrown at src/Components/Components/src/ComponentFactory.cs:81

        {
            // Typical case where no rendermode is specified in either location. We don't call ResolveComponentForRenderMode in this case.
            component = _componentActivator.CreateInstance(componentType);
        }
        else
        {
            // At least one rendermode is specified. We require that it's exactly one, and use ResolveComponentForRenderMode with it.
            var effectiveRenderMode = callerSpecifiedRenderMode is null
                ? componentTypeRenderMode!
                : componentTypeRenderMode is null
                    ? callerSpecifiedRenderMode
                    : throw new InvalidOperationException($"The component type '{componentType}' has a fixed rendermode of '{componentTypeRenderMode}', so it is not valid to specify any rendermode when using this component.");
            component = _renderer.ResolveComponentForRenderMode(componentType, parentComponentId, _componentActivator, effectiveRenderMode);
        }

        if (component is null)
        {
            // The default activator/resolver will never do this, but an externally-supplied one might
            throw new InvalidOperationException($"The component activator returned a null value for a component of type {componentType.FullName}.");
        }

        if (!_propertyInjectionDisabled)
        {
            PerformPropertyInjection(serviceProvider, component);
        }

        return component;
    }

    private void PerformPropertyInjection(IServiceProvider serviceProvider, IComponent instance)
    {
        // Suppressed with "pragma warning disable" so ILLink Roslyn Anayzer doesn't report the warning.
#pragma warning disable IL2072 // 'componentType' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'IComponentPropertyActivator.GetActivator(Type)'.
        var propertyActivator = _propertyActivator.GetActivator(instance.GetType());
#pragma warning restore IL2072

        propertyActivator(serviceProvider, instance);

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Make your custom IComponentActivator.CreateInstance never return null — fall back to Activator.CreateInstance or throw a descriptive exception.
  2. If using a custom activator as a wrapper, delegate to the default activator for unknown types.
  3. Register the activator correctly in DI so it handles all component types in use.

Example fix

// before
public class MyActivator : IComponentActivator
{
    public IComponent CreateInstance(Type t)
        => _cache.TryGetValue(t, out var c) ? c : null; // returns null -> throws
}

// after
public class MyActivator(IComponentActivator defaultActivator) : IComponentActivator
{
    public IComponent CreateInstance(Type t)
        => _cache.TryGetValue(t, out var c) ? c : defaultActivator.CreateInstance(t);
}
Defensive patterns

Strategy: try-catch

Validate before calling

var instance = _customActivator.CreateInstance(componentType);
if (instance is null)
    throw new InvalidOperationException($"Activator returned null for {componentType}.");

Try / catch

try { component = _componentActivator.CreateInstance(componentType); }
catch (InvalidOperationException ex) when (ex.Message.Contains("returned a null value"))
{
    logger.LogError(ex, "Custom component activator returned null for {Type}", componentType);
    throw;
}

Prevention

When it happens

Trigger: Registering a custom IComponentActivator whose CreateInstance returns null for some component type — e.g., a factory that returns null when it doesn't recognize a type instead of falling back.

Common situations: Custom DI/activator for view-model-style components that has a missing fallback; conditional activation logic that returns default instead of throwing or delegating.

Related errors


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