Search code examples
aspnetboilerplatefluentvalidation

FluentValidation on ApplicationService Endpoint


I'm trying to add FluentValidation to my ABP ApplicationService as described in this article.

I added the NuGet package, and also specified the dependency on my main application module:

[DependsOn(
    typeof(MyCoreModule),
    typeof(AbpQuartzModule),
    typeof(AbpFluentValidationModule))]
public class MyApplicationModule : AbpModule
{
    // ...
}

I then created a validator:

public class MyDtoValidator : AbstractValidator<MyDto>
{
    public MyDtoValidator()
    {
        RuleFor(x => x).Custom(MyCustomRule);
    }
    
    // ...
}

Then in my app service I simply have the following endpoint:

public class MyAppService : ApplicationService
{
    // Constructor

    public void MyEndpoint(MyDto input)
    {
       // ...
    }
}

The MyDtoValidator constructor is never called. I presume one has to call the validator manually for application services.


Solution

  • aspnetboilerplate resolves the validators using IOC check source code

    In the source code for the related unit tests FluentValidationTestController they defined the validator class inside the controller.

    I'm assuming that you created 2 seperate classes for the validator and controller, thus the validator factory isn't finding the MyDtoValidator class.

    TLDR: try adding the interface ITransientDependency to the MyDtoValidator class