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}'. The service provider does not implement '{nameof(IKeyedServiceProvider)}' and therefore cannot provide keyed services. What it means
Thrown by DefaultComponentPropertyActivator when a component property is decorated with [Inject(Key = someKey)] (requesting a keyed service) but the active IServiceProvider does not implement IKeyedServiceProvider. Blazor's default property injection calls GetService, but keyed injection requires a provider that supports GetKeyedService (added in .NET 8). The check is at DefaultComponentPropertyActivator.cs:79-87.
Source
Thrown at src/Components/Components/src/DefaultComponentPropertyActivator.cs:83
{
return static (_, _) => { };
}
return Initialize;
// Return an action whose closure can write all the injected properties
// 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}'.");
}
View on GitHub (pinned to 294cab2f9b)
Solutions
- Ensure the application uses the default Microsoft.Extensions.DependencyInjection ServiceProvider which supports keyed services (.NET 8+).
- If using a custom IServiceProvider, wrap it so it implements IKeyedServiceProvider.
- If keyed services are unavailable, switch the [Inject] to a non-keyed registration.
Example fix
// before
[Inject(Key = "primary")] public IMyService Svc { get; set; }
// host: services.AddSingleton(typeof(IMyService), defaultImpl); // no key, provider lacks keyed support
// after — register as keyed and ensure default ServiceProvider
services.AddKeyedSingleton<IMyService>("primary", (sp, k) => new PrimaryImpl()); Defensive patterns
Strategy: validation
Validate before calling
if (serviceKey is not null && serviceProvider is not IKeyedServiceProvider)
throw new InvalidOperationException("Provider does not support keyed services."); Type guard
static bool SupportsKeyedServices(IServiceProvider sp) => sp is IKeyedServiceProvider;
Prevention
- Use the default Microsoft.Extensions DI container (supports keyed services in .NET 8+).
- Avoid [Inject(Key=...)] if your host uses a custom non-keyed provider.
When it happens
Trigger: Using [Inject(Key = "myKey")] on a Blazor component property when the DI container does not support keyed services (e.g., a custom IServiceProvider, an older hosting setup, or a test harness with a plain ServiceProvider).
Common situations: Using keyed services in a Blazor component but the host's service provider isn't the built-in DI container that supports keys; running component unit tests with a minimal service provider; misconfiguring the host provider factory.
Related errors
- Cannot provide a value for property '{propertyName}' on type
- Cannot provide a value for property '{propertyName}' on type
- Cannot notify about changes because the {GetType()} is confi
- The component activator returned a null value for a componen
- The type {componentType.FullName} does not implement {nameof
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/5ba4b29e8f7a9da7.
Report an issue: GitHub.