我給出錯誤和構建的代碼行:
var app = builder.Build();
我的 ApplicationServiceRegister 類:
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CacheRemovingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
services.AddScoped<IAuthService, AuthManager>();
services.AddScoped<IUserService, UserManager>();
services.AddSingleton<LoggerServiceBase, FileLogger>();
return services;
}
錯誤輸出:
System.AggregateException:'無法構造某些服務(驗證服務描述符時出錯'ServiceType:MediatR.IRequestHandler 2[Application.Feature.Auths.Commands.Register.RegisterCommand,Application.Feature.Auths.Dtos.RegisteredDto] Lifetime: Transient ImplementationType: Application.Feature.Auths.Commands.Register.RegisterCommand RegisterCommandHandler': Unable to resolve service for type 'Core.Security.JWT.TokenOptions' while attempting to activate 'Application.Service.AuthService.AuthManager'.) (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler2
我詳細討論了依賴注入存在錯誤的可能性,但我沒有發現問題。
uj5u.com熱心網友回復:
您AuthManager可能在建構式中需要一個TokenOptions未注冊的引數。
如果你想完成這項作業,你還必須TokenOptions在 DI 中注冊,然后再將 DI 添加AuthManager到 DI。
var options = new TokenOptions(...);
services.AddSingleton(typeof(TokenOptions), options);
uj5u.com熱心網友回復:
就像@rotgers 說的那樣,您可能缺少一些AuthManager預期的建構式引數。您需要在服務容器中注冊所述引數,以便它可用于AuthManager.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/525084.html
下一篇:找到陣列的主要元素
