dotnet/aspnetcore · error · InvalidOperationException

Cannot provide a value for property '{propertyName}' on type

Error message

Cannot provide a value for property '{propertyName}' on type '{type.FullName}'. There is no registered keyed service of type '{propertyType}' with key '{serviceKey}'.

What it means

Thrown by DefaultComponentPropertyActivator when a component property uses [Inject(Key = someKey)] and the service provider supports keyed services, but no service of the requested type is registered with that specific key. GetKeyedService returns null, triggering the throw at DefaultComponentPropertyActivator.cs:89-92.

Source

Thrown at src/Components/Components/src/DefaultComponentPropertyActivator.cs:90

        // without any further reflection calls (just typecasts)
        void Initialize(IServiceProvider serviceProvider, IComponent component)
        {
            foreach (var (propertyName, propertyType, setter, serviceKey) in injectables)
            {
                object? serviceInstance;

                if (serviceKey is not null)
                {
                    if (serviceProvider is not IKeyedServiceProvider keyedServiceProvider)
                    {
                        throw new InvalidOperationException($"Cannot provide a value for property " +
                            $"'{propertyName}' on type '{type.FullName}'. The service provider " +
                            $"does not implement '{nameof(IKeyedServiceProvider)}' and therefore " +
                            $"cannot provide keyed services.");
                    }

                    serviceInstance = keyedServiceProvider.GetKeyedService(propertyType, serviceKey)
                        ?? throw new InvalidOperationException($"Cannot provide a value for property " +
                        $"'{propertyName}' on type '{type.FullName}'. There is no " +
                        $"registered keyed service of type '{propertyType}' with key '{serviceKey}'.");
                }
                else
                {
                    serviceInstance = serviceProvider.GetService(propertyType)
                        ?? throw new InvalidOperationException($"Cannot provide a value for property " +
                        $"'{propertyName}' on type '{type.FullName}'. There is no " +
                        $"registered service of type '{propertyType}'.");
                }

                setter.SetValue(component, serviceInstance);
            }
        }
    }
}

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Register the keyed service with the exact key and type: services.AddKeyedTransient<IMyService>("x", ...).
  2. Double-check the key value (string equality, case sensitivity) matches between registration and [Inject(Key=...)].
  3. Ensure the service interface type matches the property type exactly.

Example fix

// before
[Inject(Key = "readOnly")] public IMyRepo Repo { get; set; }
// no matching registration

// after
services.AddKeyedScoped<IMyRepo>("readOnly", (sp, k) => new ReadOnlyRepo());
Defensive patterns

Strategy: validation

Validate before calling

if (serviceProvider is IKeyedServiceProvider ksp && ksp.GetKeyedService(propertyType, serviceKey) is null)
    throw new InvalidOperationException($"No keyed service {propertyType} with key {serviceKey}.");

Type guard

static bool IsKeyRegistered(IServiceProvider sp, Type t, object key)
    => sp is IKeyedServiceProvider ksp && ksp.GetKeyedService(t, key) is not null;

Prevention

When it happens

Trigger: Decorating a property with [Inject(Key = "x")] where no AddKeyedSingleton/Scoped/Transient<IMyService>("x", ...) registration exists, or the key value doesn't match (case, type, or name mismatch).

Common situations: Renaming or removing a keyed registration without updating component attributes; key value mismatch (e.g., using an enum vs string); conditional registration that doesn't always add the key.

Related errors


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