dotnet/aspnetcore · error · InvalidOperationException

The component activator returned a null value for a…

Error message

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

What it means

A custom IComponentActivator.CreateInstance returned null for a component type. The default activator never does this, but a user-supplied or DI-based activator might. ComponentFactory treats a null result as a fatal misconfiguration.

Solutions

  1. Fix the custom activator to never return null; throw on unknown types instead.
  2. Fall back to the default activation path when the custom path cannot resolve the type.
  3. Add null-check logging in the activator to catch which type fails to resolve.

Example fix

// before
public IComponent CreateInstance(Type t) => _custom.TryGetValue(t, out var c) ? c : null;
// after
public IComponent CreateInstance(Type t) => _custom.TryGetValue(t, out var c) ? c : _default.CreateInstance(t);
Defensive patterns

Strategy: try-catch

Validate before calling

// Inspect the activator contract; ensure it never returns null.
if (activator.CreateInstance(type) is null) throw new InvalidOperationException("Activator returned null.");

Try / catch

try { component = factory.CreateComponent(type, ...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("returned a null value"))
{
    _logger.LogError(ex, "Custom activator returned null for {Type}", type);
    throw;
}

Prevention

When it happens

Trigger: Custom IComponentActivator that returns null for some component types, or a DI container resolving to null due to a missing or failing registration.

Common situations: Buggy custom activator, conditional activation logic that falls through without returning, or a DI scope/lifetime mismatch causing resolution to fail silently.

Related errors


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

Appendix: 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 3600ca084e)