dotnet/aspnetcore · error · InvalidOperationException

Cannot provide a value for property

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

The service provider implements IKeyedServiceProvider, but GetKeyedService returned null for the requested type+key pair. No matching keyed registration exists, which means [Inject(Key=...)] cannot be satisfied.

Solutions

  1. Register the keyed service with the exact key: services.AddKeyedSingleton<T, TImpl>("myKey").
  2. Verify the key value and type match between registration and [Inject(Key=...)].
  3. Check registration lifetime matches the consuming scope.

Example fix

// before
[Inject(Key = "admin")] IUserService Svc { get; set; }
// no registration
// after
builder.Services.AddKeyedSingleton<IUserService, AdminUserService>("admin");
Defensive patterns

Strategy: validation

Validate before calling

// Resolve the keyed service at startup to fail fast if missing.
var svc = sp.GetKeyedService<IMyService>("myKey");
if (svc is null) throw new InvalidOperationException("Keyed service 'myKey' not registered.");

Type guard

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

Prevention

When it happens

Trigger: [Inject(Key = "myKey")] on a property, but no services.AddKeyedSingleton<T,TImpl>("myKey") (or scoped/transient) registration exists.

Common situations: Key typo between registration and injection, forgot to register the keyed service, key type mismatch (e.g. string vs enum), or conditional registration not met.

Related errors


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

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