OrchardCMS/OrchardCore · error · InvalidOperationException

This service provider doesn't support keyed services.

Error message

This service provider doesn't support keyed services.

What it means

This extension resolves a keyed service from an arbitrary IServiceProvider by casting to IKeyedServiceProvider. If the provider is a plain IServiceProvider that does not implement keyed-service support (pre-.NET 8 container or a custom/minimal provider), it throws InvalidOperationException instead of returning null.

Solutions

  1. Ensure the service provider is the Microsoft.Extensions.DependencyInjection container from .NET 8+ which implements IKeyedServiceProvider.
  2. Register the service with a key: services.AddKeyedSingleton<TService, TImpl>("key") so resolution makes sense.
  3. If keyed support may be absent, check 'provider is IKeyedServiceProvider' before resolving and fall back to a non-keyed lookup.
  4. Replace custom/fake providers in tests with the real ServiceCollection provider or an IKeyedServiceProvider mock.

Example fix

// before
var svc = provider.GetKeyedService(typeof(IMailer), "smtp");
// after
IMailer svc = provider is IKeyedServiceProvider ksp
    ? (IMailer)ksp.GetKeyedService(typeof(IMailer), "smtp")
    : provider.GetRequiredService<IMailer>();
Defensive patterns

Strategy: type-guard

Validate before calling

if (provider is not IKeyedServiceProvider) throw new NotSupportedException("Keyed services are not supported by this provider.");

Type guard

bool SupportsKeyedServices(IServiceProvider p) => p is IKeyedServiceProvider;

Try / catch

try { svc = provider.GetKeyedService(typeof(T), key); }
catch (InvalidOperationException ex) when (ex.Message.Contains("keyed services")) { svc = provider.GetService<T>(); }

Prevention

When it happens

Trigger: Calling provider.GetKeyedService(typeof(T), key) or GetRequiredKeyedService on an IServiceProvider whose concrete type (e.g. a fake, a wrapper, or a container built without keyed support) is not IKeyedServiceProvider.

Common situations: Unit tests using Moq/FakeServiceProvider stubs, third-party DI containers lacking keyed-service support, or calling the extension on a scoped provider built from an old adapter.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/b3e1d967222a4590. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/ServiceProviderExtensions.cs:37

    /// </summary>
    public static TResult CreateInstance<TResult>(this IServiceProvider provider, Type type) where TResult : class
    {
        return (TResult)ActivatorUtilities.CreateInstance(provider, type);
    }

#if !NET10_0_OR_GREATER
    /// <summary>
    /// Gets the service object of the specified type with the specified key.
    /// </summary>
    public static object? GetKeyedService(this IServiceProvider provider, Type serviceType, object? serviceKey)
    {
        ArgumentNullException.ThrowIfNull(provider);
        if (provider is IKeyedServiceProvider keyedServiceProvider)
        {
            return keyedServiceProvider.GetKeyedService(serviceType, serviceKey);
        }

        throw new InvalidOperationException("This service provider doesn't support keyed services.");
    }
#endif

}

View on GitHub (pinned to 4306c0717f)