dotnet/aspnetcore · error · InvalidOperationException

Cannot provide a value for property

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

A property decorated with [Inject] specifies a Key, which requires keyed-service resolution. The active IServiceProvider does not implement IKeyedServiceProvider, so keyed resolution is impossible. Keyed services require .NET 8+ and a compatible host.

Solutions

  1. Upgrade to .NET 8+ and use the default host which supports keyed services.
  2. Ensure the service provider passed to the renderer implements IKeyedServiceProvider.
  3. Remove the Key from [Inject] and register the service as non-keyed if keyed resolution is unavailable.

Example fix

// before
[Inject(Key = "admin")] IUserService UserService { get; set; }
// host pre-.NET8
// after (option A)
[Inject] IUserService UserService { get; set; }  // non-keyed registration
Defensive patterns

Strategy: validation

Validate before calling

// Check provider capability before relying on keyed services.
if (serviceProvider is not IKeyedServiceProvider && injectHasKey) { /* avoid keyed [Inject] or upgrade host */ }

Type guard

static bool SupportsKeyedServices(IServiceProvider sp) => sp is IKeyedServiceProvider;

Prevention

When it happens

Trigger: [Inject(Key = "myKey")] on a component property, but the service provider (e.g. an older or custom container) does not implement IKeyedServiceProvider.

Common situations: Running on .NET 7 or earlier, a custom/legacy ServiceProvider, or a host that wraps the provider and loses the IKeyedServiceProvider interface.

Related errors


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

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