My line of code that gives errors and builds:
var app = builder.Build();
My ApplicationServiceRegister class:
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;
}
Error Output:
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler2[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.IRequestHandler
2
I dwelt on the possibility of a bug with Dependency Injection, but I didn't see a problem.
CodePudding user response:
Your AuthManager
probably requires a parameter in the constructor TokenOptions
, which is not registered.
If you want to make this work you also have to register the TokenOptions
in the DI, before adding the AuthManager
to the DI.
var options = new TokenOptions(...);
services.AddSingleton(typeof(TokenOptions), options);
CodePudding user response:
Like @rotgers said you are probably missing some constructor parameter that the AuthManager
is expecting. You need to register said parameter in the services container so it is available for AuthManager
.