Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ServiceStack+;FluentValidation未使用ResolveService触发_C#_Asp.net Mvc_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack_Servicestack Bsd - Fatal编程技术网 servicestack,servicestack-bsd,C#,Asp.net Mvc,servicestack,Servicestack Bsd" /> servicestack,servicestack-bsd,C#,Asp.net Mvc,servicestack,Servicestack Bsd" />

C# ServiceStack+;FluentValidation未使用ResolveService触发

C# ServiceStack+;FluentValidation未使用ResolveService触发,c#,asp.net-mvc,servicestack,servicestack-bsd,C#,Asp.net Mvc,servicestack,Servicestack Bsd,我正在使用v3 我可以直接发布到API并体验请求验证,但是,当从我的MVC控制器中已解析的服务实例调用时,不会触发任何验证 使用Fiddler,我发布以下内容: 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

我正在使用v3

我可以直接发布到API并体验请求验证,但是,当从我的MVC控制器中已解析的服务实例调用时,不会触发任何验证

使用Fiddler,我发布以下内容:

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");
        }
    }
使用(var svc=AppHostBase.ResolveService(System.Web.HttpContext.Current))
{
试试{svc.Post(model.Sample);}
捕获(WebServiceException webEx)
{
返回Json(新的{Success=false},“text/html”);
}
}
没有抛出异常

在服务中手动创建IValidator实例并引发异常不会引发异常



为什么验证没有触发来自
AppHostBase.ResolveService
的请求?

我在这里有两个发现,不过我想看看是否有人对源代码有更全面的了解,可以确认或反驳它们


AppHostBase.ResolveService与JsonServiceClient的对比

将我的连接方法更改为以下将触发验证

using (var client = new JsonServiceClient(baseUri)) {
    client.Post(model.Sample);
}
AppHostBase.ResolveService
只是从IoC容器返回一个实例(就像它应该返回的那样),但我的假设是,该实例仍将使用触发验证的相同请求过滤器/管道。这是有道理的,我并不是越想它,但在考虑请求过滤器验证方法时,它的行为显然并不明显


IReturnVoid防止WebService异常


一旦我跨越了这个障碍,我就很难找回FluentValidation错误消息。显示将抛出包含验证详细信息的
webserviceeexception
,但我得到的是
WebException
。我终于意识到我的请求DTO正在实现
IReturnVoid
。这似乎决定了引发哪种类型的异常。将其更改为
IReturn
会根据需要引发
WebServiceException

为了澄清,您提到的
WebServiceException
与服务客户端相关,对吗?因此,当服务客户端看到请求DTO为
IReturnVoid
时,很可能是该客户端使验证失败异常不可用。在服务器端,
IReturnVoid
标记不应更改是否一般针对验证失败返回错误响应。关于
webserviceeexception
,是。看起来客户确实在控制它。但是,潜在的问题是,在使用已解析的服务引用时,根本不会触发验证。RequestFilter验证方案允许实现假设传入的请求是有效的,因此任何偏离这一点都可能导致一些实际问题。希望我只是误用了它或者误解了,因为我真的希望它能这样工作。是的,这是正确的。我只是想向那些可能感兴趣的人强调一些正在发生的事情的细节。