Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentValidation.AspNetCore is not working in Class Library

I am using library "FluentValidation.AspNetCore": "6.4.0-beta3" in .netcore WebApi in a project. You can see the project structure below. Library is working fine if i place the CurrencyDTO.cscode in section 2 (Project FH.WebAPI) and if the same code placed in section 1 (Class Library DTO) its not working. And requirement is that i have to place code in Class library FH.Common. Is there any work around.I have search but didn't find any thing

Project Structure

Project Structure

CurrencyDTO.cs

[Validator(typeof(CurrencyDTOValidator))]
    public class CurrencyDTO
    {
        public int Id { get { return CurrencyId; } }
        public int CurrencyId { get; set; }
        public string Name { get; set; }
        public string Symbol { get; set; }

    }

    public class CurrencyDTOValidator : AbstractValidator<CurrencyDTO>
    {
        public CurrencyDTOValidator()
        {
            RuleFor(x => x.Name).NotEmpty().NotNull().WithMessage("The currency 'Name' is required.")
                                      .Length(0, 250).WithMessage("The currency 'Name' cannot be more than 250 characters.");

            RuleFor(x => x.Symbol).NotEmpty().WithMessage("The currency Symbol is required.");


        }
    }

Library Configuration

Step 1) . Add in project.json

{
  "dependencies": {
    "Autofac": "4.3.0",
    "Autofac.Extensions.DependencyInjection": "4.0.0",
    "AutoMapper": "5.2.0",
    "EntityFramework": "6.1.3",
    "FH.Business": "1.0.0-*",
    "FH.Common": "1.0.0-*",
    "JWT": "1.4.1-beta",
    "Microsoft.AspNet.Http.Extensions": "1.0.0-rc1-final",
    "Microsoft.AspNet.WebApi.Client": "5.2.3",
    "Microsoft.AspNet.WebApi.Core": "5.2.3",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.1.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.1",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    //other libraries..
    "FluentValidation.AspNetCore": "6.4.0-beta3" //<------Here 
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net461": {
      "frameworkAssemblies": {
        "System.Drawing": "4.0.0.0"
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Step 2). In Startup.cs

   public void ConfigureServices(IServiceCollection services)
            {
                // Add framework services.
                services.AddMvc(options => {  }).AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
// Other code..
             }

Used in Controller

[HttpPost]
        [Route("CreateCurrency")]
        public IActionResult CreateCurrency([FromBody] CurrencyDTO model)
        {
            if (!ModelState.IsValid) //<----Validate here
            {
                return new BadRequestObjectResult(ModelState);
            }
        //Other Code..
        }
like image 251
Usman lqbal Avatar asked Feb 03 '17 14:02

Usman lqbal


1 Answers

The problem was in a registration line in startup.csand thank to @JeremySkinner who have suggested me the right way and i am quoting his answer here.

My mistake

services.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());

Replace

RegisterValidatorsFromAssemblyContaining<Startup>() 

with

RegisterValidatorsFromAssemblyContaining<CurrencyDTOValidator>()

Actual link to the Issue

Link Description

Above link is the answers posted @JeremySkinner

like image 162
Usman lqbal Avatar answered Oct 15 '22 17:10

Usman lqbal