我必须忽略使 fluent-validation 在我创建的基本 Service-Stack 应用程序中触发的事情。
我一直在遵循发现here的示例。对于我的一生,我似乎无法让我的验证员开枪????
面包屑,一定是我想念的愚蠢.... ???



要求是:
{“名称”:“”,“公司”:“公司”,“年龄”:10,“计数”:110,“地址”:“123棕色海峡。”}

回复 :
“用户已保存...”

这是代码:
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.验证程序
 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.服务
 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();
    }

最佳答案

好的...发现问题了。...我(通过nuget)安装了(错误)并在我的项目中引用了带有Service-Stack的FluentValidation实现的FluentValidation.dll(请参阅命名空间ServiceStack.FluentValidation)。
一旦删除了唯一的不正确的FluentValidation引用,并确保我的验证器从AbstractValidator的服务堆栈实现扩展,验证器就正确触发了...

关于ServiceStack和FluentValidation不触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15644546/

10-12 19:46