{"record":{"id":"b3e1d967222a4590","repo":"OrchardCMS/OrchardCore","slug":"this-service-provider-doesn-t-support-keyed-services","errorCode":null,"errorMessage":"This service provider doesn't support keyed services.","messagePattern":"This service provider doesn't support keyed services\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/ServiceProviderExtensions.cs","lineNumber":37,"sourceCode":"    /// </summary>\n    public static TResult CreateInstance<TResult>(this IServiceProvider provider, Type type) where TResult : class\n    {\n        return (TResult)ActivatorUtilities.CreateInstance(provider, type);\n    }\n\n#if !NET10_0_OR_GREATER\n    /// <summary>\n    /// Gets the service object of the specified type with the specified key.\n    /// </summary>\n    public static object? GetKeyedService(this IServiceProvider provider, Type serviceType, object? serviceKey)\n    {\n        ArgumentNullException.ThrowIfNull(provider);\n        if (provider is IKeyedServiceProvider keyedServiceProvider)\n        {\n            return keyedServiceProvider.GetKeyedService(serviceType, serviceKey);\n        }\n\n        throw new InvalidOperationException(\"This service provider doesn't support keyed services.\");\n    }\n#endif\n\n}\n","sourceCodeStart":19,"sourceCodeEnd":42,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/ServiceProviderExtensions.cs#L19-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the service provider is the Microsoft.Extensions.DependencyInjection container from .NET 8+ which implements IKeyedServiceProvider.","Register the service with a key: services.AddKeyedSingleton<TService, TImpl>(\"key\") so resolution makes sense.","If keyed support may be absent, check 'provider is IKeyedServiceProvider' before resolving and fall back to a non-keyed lookup.","Replace custom/fake providers in tests with the real ServiceCollection provider or an IKeyedServiceProvider mock."],"exampleFix":"// before\nvar svc = provider.GetKeyedService(typeof(IMailer), \"smtp\");\n// after\nIMailer svc = provider is IKeyedServiceProvider ksp\n    ? (IMailer)ksp.GetKeyedService(typeof(IMailer), \"smtp\")\n    : provider.GetRequiredService<IMailer>();","handlingStrategy":"type-guard","validationCode":"if (provider is not IKeyedServiceProvider) throw new NotSupportedException(\"Keyed services are not supported by this provider.\");","typeGuard":"bool SupportsKeyedServices(IServiceProvider p) => p is IKeyedServiceProvider;","tryCatchPattern":"try { svc = provider.GetKeyedService(typeof(T), key); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"keyed services\")) { svc = provider.GetService<T>(); }","preventionTips":["Use the Microsoft.Extensions.DependencyInjection container (.NET 8+) when relying on keyed services","Avoid hand-rolled or stub IServiceProvider in code paths that resolve keyed services","Register keyed services explicitly with AddKeyedSingleton/Scoped/Transient"],"tags":["dependency-injection","keyed-services","service-provider"],"backgroundTag":"unsupported-operation","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}