Posted: 17/04/2024
This Service Descriptor Is keyed Your Service Provider May Not Support Keyed Services
I just came across this error without any code changes.
System.InvalidOperationException HResult=0x80131509 Message=This service descriptor is keyed. Your service provider may not support keyed services. Source=Microsoft.Extensions.DependencyInjection.Abstractions StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor() at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationInstance() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory..ctor(ICollection`1 descriptors) at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services) at MediaServer.Startup.SetupRateLimitWithRetryStrategyPipeline(IServiceCollection services) in Startup.cs:line 129 at MediaServer.Startup.ConfigureServices(IServiceCollection services) in Startup.cs:line 86 at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_0.<BuildStartupServicesFilterPipeline>g__RunPipeline|0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
😨
Turns out a minor nuget package upgrade caused it (in this case
Polly
v8.0.0
to
v8.3.1
)
I found this issue on github which is exactly what I am seeing:
https://github.com/dotnet/runtime/issues/95789
It basically says; something has changed in dotnet DI registrations, and now anything that uses a "Keyed" registration is potentially going to break by throwing this exception.
This is because of a change in v8 of
Microsoft.Extensions.DependencyInjection
:
- https://weblogs.asp.net/ricardoperes/net-8-dependency-injection-changes-keyed-services
- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-8.0#keyed-services
- https://andrewlock.net/exploring-the-dotnet-8-preview-keyed-services-dependency-injection-support/
The only way I solved it was to downgrade
Polly
back to version
8.2.1
This was using a
dotnet6
app.
I don't know what else to do to work around this error, as we are not registering anything in a "Keyed" way, nor are we directly referencing v8 of
Microsoft.Extensions.DependencyInjection
. So I am concerned that we are going to be "stuck" at certain version of packages, and eventually be unable to upgrade anything. 😱
Someone please Tweet me if I am wrong!
Thanks for reading this post.
If you want to reach out, catch me on Twitter!
I am always open to mentoring people, so get in touch.