C
C#13mo ago
Noriega

Exception when attempting to use keyed dependencies

I tried making use of the new keyed DI feature that comes with net 8, but when I try injecting 2 dependencies with IServiceCollection.AddKeyedSingleton<T1, T2>(""), Im receiving an exception that I havent found any fix for. this kind of setup for dependencies doesn't seem to require any special setup according to the example on the microsoft page, so im confused Code:
serviceCollection.AddKeyedSingleton<IScraper, BooruScraper>("booru");
serviceCollection.AddKeyedSingleton<IScraper, HIScraper>("hi");
serviceCollection.AddKeyedSingleton<IScraper, BooruScraper>("booru");
serviceCollection.AddKeyedSingleton<IScraper, HIScraper>("hi");
Exception:
- System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services.
- System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services.
10 Replies
lycian
lycian13mo ago
This can happen if you have a service that does assembly scanning, similar to https://github.com/jbogard/MediatR/issues/942
GitHub
System.InvalidOperationException with KeyedServices in .NET 8 · Iss...
Hi, I'm currently playing with the new .NET 8 Preview 7 that came out a few days ago. When I add a KeyedService to the DI container, it causes a System.InvalidOperationException thrown by Media...
Noriega
NoriegaOP13mo ago
Well my exception has nothing to do with mediatr, but do you mean if Im using reflection while registering dependencies?
lycian
lycian13mo ago
If you, or someone else is, then potentially
Noriega
NoriegaOP13mo ago
got it do you have any ideas for registering 2 dependencies with the same interface, other than directly registering the implemented class?
lycian
lycian13mo ago
full stack would help a little more keyed should work, but it really depends on a lot of things. The stack at least can help identify the source of the issue
Noriega
NoriegaOP13mo ago
System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services. at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor()
at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationType()
at Microsoft.Extensions.DependencyInjection.InternalServiceCollectionExtensions.<>c__19`1.<TryAddParameterExpressionBuilder>b__19_0(ServiceDescriptor t)
at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.Extensions.DependencyInjection.InternalServiceCollectionExtensions.TryAddParameterExpressionBuilder[T](IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.InternalServiceCollectionExtensions.TryAddDataLoaderParameterExpressionBuilder(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.RequestExecutorServiceCollectionExtensions.AddGraphQLCore(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.HotChocolateAspNetCoreServiceCollectionExtensions.AddGraphQLServerCore(IServiceCollection services, Int32 maxAllowedRequestSize)
at Microsoft.Extensions.DependencyInjection.HotChocolateAspNetCoreServiceCollectionExtensions.AddGraphQLServer(IServiceCollection services, String schemaName, Int32 maxAllowedRequestSize)
at Scathach.Infrastructure.Extensions.ServiceCollectionExtensions.AddScathachApiServer(IServiceCollection serviceCollection) in C:\Users\crobati\OneDrive\Scathach\src\Scathach.Infrastructure\Extensions\ServiceCollectionExtensions.cs:line 16
at Scathach.Program.ConfigureServices(WebApplicationBuilder webApplicationBuilder) in C:\Users\crobati\OneDrive\Scathach\src\Scathach\Program.cs:line 160
at Scathach.Program.Main(String[] args) in C:\Users\crobati\OneDrive\Scathach\src\Scathach\Program.cs:line 76
at Scathach.Program.<Main>(String[] args)
System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services. at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor()
at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationType()
at Microsoft.Extensions.DependencyInjection.InternalServiceCollectionExtensions.<>c__19`1.<TryAddParameterExpressionBuilder>b__19_0(ServiceDescriptor t)
at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.Extensions.DependencyInjection.InternalServiceCollectionExtensions.TryAddParameterExpressionBuilder[T](IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.InternalServiceCollectionExtensions.TryAddDataLoaderParameterExpressionBuilder(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.RequestExecutorServiceCollectionExtensions.AddGraphQLCore(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.HotChocolateAspNetCoreServiceCollectionExtensions.AddGraphQLServerCore(IServiceCollection services, Int32 maxAllowedRequestSize)
at Microsoft.Extensions.DependencyInjection.HotChocolateAspNetCoreServiceCollectionExtensions.AddGraphQLServer(IServiceCollection services, String schemaName, Int32 maxAllowedRequestSize)
at Scathach.Infrastructure.Extensions.ServiceCollectionExtensions.AddScathachApiServer(IServiceCollection serviceCollection) in C:\Users\crobati\OneDrive\Scathach\src\Scathach.Infrastructure\Extensions\ServiceCollectionExtensions.cs:line 16
at Scathach.Program.ConfigureServices(WebApplicationBuilder webApplicationBuilder) in C:\Users\crobati\OneDrive\Scathach\src\Scathach\Program.cs:line 160
at Scathach.Program.Main(String[] args) in C:\Users\crobati\OneDrive\Scathach\src\Scathach\Program.cs:line 76
at Scathach.Program.<Main>(String[] args)
seems to be the following line when I register a gql server
serviceCollection.AddGraphQLServer()
.AddQueryType<QueryType>()
.AddType<CharacterType>();
serviceCollection.AddGraphQLServer()
.AddQueryType<QueryType>()
.AddType<CharacterType>();
lycian
lycian13mo ago
yea, likely the GraphQL library broke and will need to have something similar to this line https://github.com/jbogard/MediatR/blob/6baaeef7a16ca75cb905829f870f79c6334ef40e/src/MediatR/Registration/ServiceRegistrar.cs#L273
GitHub
MediatR/src/MediatR/Registration/ServiceRegistrar.cs at 6baaeef7a16...
Simple, unambitious mediator implementation in .NET - jbogard/MediatR
lycian
lycian13mo ago
GitHub
Add Keyed Services Support to Dependency Injection by benjaminpetit...
Related issue: #64427 Since my last proposal (https://gist.github.com/benjaminpetit/49a6b01692d0089b1d0d14558017efbc) we made some changes and took some decisions. This PR is still in draft, but ev...
lycian
lycian13mo ago
so you're doing the right thing, the library you're using isn't
Noriega
NoriegaOP13mo ago
@Andrew (ryzngard | they/them) I was meant to update on this way earlier, but i needed to update a graphql package in my project turns out I was supposed to update it from nuget

Did you find this page helpful?