自定义异常最终所有的 ServiceStack WebServiceException 都只是带有填充 ResponseStatus 与 HTTP 错误状态一起返回.有多种不同的方法可以自定义返回异常的方式,包括:启用堆栈跟踪默认情况下,在响应 DTO 中显示 StackTraces 仅在调试版本中启用,尽管此行为可通过以下方式覆盖:csharpSetConfig(new HostConfig { DebugMode = true });Is ResponseStatus needed?The wiki says that we need to have a ResponseStatus property in our response DTO to handle exception serialization: https://github.com/ServiceStack/ServiceStack/wiki/Validation However it looks like ResponseStatus is generated automatically even if there is no ResponseStatus property in the response DTO.Do we need the ResponseStatus property? 解决方案 The Error Handling Docs explains how you can control which Services return a populated ResponseStatus DTO, i.e:Error Response TypesThe Error Response that gets returned when an Exception is thrown varies on whether a conventionally-named {RequestDto}Response DTO exists or not. If it exists:The {RequestDto}Response is returned, regardless of the service method's response type. If the {RequestDto}Response DTO has a ResponseStatus property, it is populated otherwise no ResponseStatus will be returned. (If you have decorated the {ResponseDto}Response class and properties with [DataContract]/[DataMember] attributes, then ResponseStatus also needs to be decorated, to get populated).Otherwise, if it doesn't:A generic ErrorResponse gets returned with a populated ResponseStatus property.The Service Clients transparently handles the different Error Response types, and for schema-less formats like JSON/JSV/etc there's no actual visible difference between returning a ResponseStatus in a custom or generic ErrorResponse - as they both output the same response on the wire.Custom ExceptionsUltimately all ServiceStack WebServiceExceptions are just Response DTO's with a populated ResponseStatus that are returned with a HTTP Error Status. There are a number of different ways to customize how Exceptions are returned including:Enabling StackTracesBy default displaying StackTraces in Response DTOs are only enabled in Debug builds, although this behavior is overridable with:csharpSetConfig(new HostConfig { DebugMode = true }); 这篇关于ServiceStack 中是否需要 ResponseStatus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 19:10