我编写了一个接口并在 3 个服务中实现了它,但是当我使用
AddKeyedScoped
将它们注入到项目中时,出现以下错误。
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
代码:
services.AddKeyedScoped<IEPaymentService, SamanService>(BackofficeOptions.SamanBankService);
services.AddKeyedScoped<IEPaymentService, MellatService>(BackofficeOptions.MellatBankService);
services.AddKeyedScoped<IEPaymentService, MelliService>(BackofficeOptions.MelliBankService);
您可以在最后添加所有键控依赖项作为解决方法。