Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack and FluentValidation NOT firing

I must be overlooking something around getting the fluent-validation to fire within basic Service-Stack application I created. I have been following the example found here. For the life of me I can't seem to get my validators fire???? Crumbs, there must be something stupid that I'm missing....???

I'm issuing a user request against the User-Service (http://my.service/users), the request goes straight through without invoking the appropriate validator registered.

Request is : {"Name":"","Company":"Co","Age":10,"Count":110,"Address":"123 brown str."}

Response : "user saved..."


Here is the code : 1.DTO

[Route("/users")]
public class User
{
    public string Name { get; set; }
    public string Company { get; set; }
    public int Age { get; set; }
    public int Count { get; set; }
    public string Address { get; set; }
}

2.Validator

 public class UserValidator : AbstractValidator<User>
{
    public UserValidator()
    {
        RuleFor(r => r.Name).NotEmpty();
        RuleFor(r => r.Age).GreaterThan(0);
    }
}

3.AppHostBase

public class ValidationAppHost : AppHostBase
{
    public ValidationAppHost()
        : base("Validation Test", typeof(UserService).Assembly)
    {

    }

    public override void Configure(Funq.Container container)
    {
        Plugins.Add(new ValidationFeature());

        //This method scans the assembly for validators
        container.RegisterValidators(typeof(UserValidator).Assembly);
    }
}

4.Service

 public class UserService : Service
{
    public object Any(User user)
    {
        return "user saved...";
    }
}

5.Global.asax.cs

protected void Application_Start(object sender, EventArgs e)
    {
        new ValidationAppHost().Init();
    }
like image 707
darthal Avatar asked Mar 26 '13 18:03

darthal


1 Answers

Ok....found the issue....I (in error) installed (via nuget) and referenced within my project the FluentValidation.dll with Service-Stack's FluentValidation implementation (see namespace ServiceStack.FluentValidation). Once I removed this the sole incorrect FluentValidation reference and ensured that my validator extended from the service-stack implementation of the AbstractValidator the validators fired correctly...

like image 85
darthal Avatar answered Oct 10 '22 01:10

darthal