Keyed service dependency problem when injecting in Program.cs

293 Views Asked by At

I wrote a interface and implemented it in 3 services, but when I used AddKeyedScoped to inject these to project I get the following error.

Unhandled exception. 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 MediatR.Registration.ServiceRegistrar.<>c.<RegisterBehaviorIfImplementationsExist>b__12_0(ServiceDescriptor service)
   at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
   at System.Linq.Enumerable.OfTypeIterator[TResult](IEnumerable source)+MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at MediatR.Registration.ServiceRegistrar.RegisterBehaviorIfImplementationsExist(IServiceCollection services, Type behaviorType, Type subBehaviorType)
   at MediatR.Registration.ServiceRegistrar.AddRequiredServices(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, MediatRServiceConfiguration configuration)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, Action`1 configuration)
   at Program.<Main>$(String[] args) in D:\Soshyant\farabixo-middlewares\src\FarabixoMiddleware\Program.cs:line 51

Code:

services.AddKeyedScoped<IEPaymentService, SamanService>(BackofficeOptions.SamanBankService);
services.AddKeyedScoped<IEPaymentService, MellatService>(BackofficeOptions.MellatBankService);
services.AddKeyedScoped<IEPaymentService, MelliService>(BackofficeOptions.MelliBankService);
0

There are 0 best solutions below