'FluentValidation Reddit Clone No Service for Type
I'm trying to get this API (.net core 3.1) up and running https://github.com/EddieAbbondanzio/Updog.in
But I keep getting this error when trying to login
No service for type 'Updog.Application.LoginUserCommandValidator' has been registered.
The error occurs in the
Updog.Application\Core\CQRS\Command\ComandHandler.cs file
On this line (i had to add a try/catch to catch the error):
validator = provider.GetRequiredService(validateAttribute.Validator) as IValidator;
It involves Fluentvalidation 8.5 implemented with dependency injection and uses generics and CRQS design pattern to execute the login validation. The dependency injection seems to be setup correctly in the configureservices method in startup.cs
services.AddScoped<CommandHandler, LoginUserCommandHandler>(); services.AddTransient<IValidator, LoginUserCommandValidator>();
Here is the stack trace
System.InvalidOperationException: No service for type 'Updog.Application.LoginUserCommandValidator' has been registered.
at Updog.Application.Mediator.Command[TCommand](TCommand command) in \Updog.Application\Core\CQRS\Mediator.cs:line 36 at Updog.Api.SessionController.Login(SessionLoginRequest loginRequest) in Updog.Api\Session\SessionController.cs:line 46 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Steps to recreate:
download the github zip file from here https://github.com/EddieAbbondanzio/Updog.in
Open the Updog.sln solution file in visual studio, be sure to have .net core 3.1 installed
Create an appsettings.json file in the Updog.api folder, and attach it to the project
add the following to the appsettings file
{ "Database": { "Host": "", "Port": 5432, "User": "", "Password": "", "Database": "" }, "AuthenticationToken": { "Secret": "secret", "Expires": 7776000, "Issuer": "updog.in" }, "Admin": { "Username": "", "Password": "" }}
set the updog.api project as the startup project, run the project
set a break point on the line mentioned in this file Updog.Application\Core\CQRS\Command\ComandHandler.cs file
On this line validator = provider.GetRequiredService(validateAttribute.Validator) as IValidator; (i had to add a try/catch to catch the error)
disable the db migration so you don't need the postgres by commenting out this line code to comment out. The file is here \Updog.Api\Program.cs
open postman and use this postman json to post to the login endpoint
{ "info": { "_postman_id": "c8b81c40-d12f-4a36-bb6b-90712095c871", "name": "Updog", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "Login", "request": { "method": "POST", "header": [], "body": { "mode": "raw", "raw": "{ \"username\":\"asdfd\",\"password\":\"asdf\"}", "options": { "raw": { "language": "json" } } }, "url": { "raw": "https://localhost:44398/api/session/", "protocol": "https", "host": [ "localhost" ], "port": "44398", "path": [ "api", "session", "" ] } }, "response": [] } ]}
Here's what I've tried:
- I've search all over stackoverflow for posts that have the same error but nothing was related
- Read all the fluentvalidation docs
- Read the asp.net core dependency injection docs
- Read the CQRS Pattern docs
- Posted an issue on the github site, no response
- Emailed the developer directly, no response
Any help would be greatly appreciated, thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
