dotnet/aspnetcore · error · ArgumentNullException

componentActivator

Error message

componentActivator

What it means

ComponentFactory's constructor requires a non-null IComponentActivator. This is a standard null-guard precondition (ArgumentNullException) ensuring the factory can always activate components.

Source

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

        isDisabled;

    private static readonly ConcurrentDictionary<Type, IComponentRenderMode?> _cachedComponentTypeRenderModes = new();

    static ComponentFactory()
    {
        if (HotReloadManager.IsSupported)
        {
            HotReloadManager.Default.OnDeltaApplied += ClearCache;
        }
    }

    private readonly IComponentActivator _componentActivator;
    private readonly IComponentPropertyActivator _propertyActivator;
    private readonly Renderer _renderer;

    public ComponentFactory(IComponentActivator componentActivator, IComponentPropertyActivator propertyActivator, Renderer renderer)
    {
        _componentActivator = componentActivator ?? throw new ArgumentNullException(nameof(componentActivator));
        _propertyActivator = propertyActivator ?? throw new ArgumentNullException(nameof(propertyActivator));
        _renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
    }

    public static void ClearCache() => _cachedComponentTypeRenderModes.Clear();

    private static IComponentRenderMode? GetComponentTypeRenderMode([DynamicallyAccessedMembers(Component)] Type componentType)
    {
        // Unfortunately we can't use 'GetOrAdd' here because the DynamicallyAccessedMembers annotation doesn't flow through to the
        // callback, so it becomes an IL2111 warning. The following is equivalent and thread-safe because it's a ConcurrentDictionary
        // and it doesn't matter if we build a cache entry more than once.
        if (!_cachedComponentTypeRenderModes.TryGetValue(componentType, out var renderMode))
        {
            renderMode = componentType.GetCustomAttribute<RenderModeAttribute>()?.Mode;
            _cachedComponentTypeRenderModes.TryAdd(componentType, renderMode);
        }

        return renderMode;

View on GitHub (pinned to 3600ca084e)

Solutions

  1. Ensure IComponentActivator is registered in the service collection.
  2. Pass a non-null instance, e.g. DefaultComponentActivator, when constructing manually.
  3. Verify DI resolution before constructing the factory in test setup.

Example fix

// before
var factory = new ComponentFactory(null, propActivator, renderer);
// after
var factory = new ComponentFactory(new DefaultComponentActivator(sp), propActivator, renderer);
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(componentActivator);
var factory = new ComponentFactory(componentActivator, propertyActivator, renderer);

Prevention

When it happens

Trigger: Calling new ComponentFactory(null, propertyActivator, renderer), or DI failing to resolve IComponentActivator and passing null.

Common situations: DI misconfiguration where IComponentActivator is not registered, manual construction in tests with a null mock, or a custom DI container that returns null for unregistered services.

Related errors


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