我正在使用ServiceStack + FluentValidation v3。
我可以直接发布到 API 并体验请求验证,但是,当从我的 MVC 控制器中已解析的服务实例调用时,不会触发验证。
使用 Fiddler,我POST
执行以下操作:
POST /api/json/oneway/FieldSample HTTP/1.1
Content-Type: application/json
Content-Length: 66
Host: localhost:53185
{"Sample.Id":"2866246","Sample.SampleTime":"6/7/1950 12:00:00 PM"}
响应,根据需要:
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 138
Connection: Close
{"responseStatus":{"errorCode":"LessThan","message":"TestTest","errors":[{"errorCode":"LessThan","fieldName":"Id","message":"TestTest"}]}}
从我的 MVC 控制器:
using (var svc = AppHostBase.ResolveService<FieldSampleService>(System.Web.HttpContext.Current))
{
try { svc.Post(model.Sample); }
catch (WebServiceException webEx)
{
return Json(new { Success = false }, "text/html");
}
}
不会抛出异常。
在服务中手动创建 IValidator 的实例并引发异常确实会冒泡异常。
为什么验证不会针对来自 的请求触发AppHostBase.ResolveService
?