ServiceStack中是否需要ResponseStatus? [英] Is ResponseStatus needed in ServiceStack?

查看:179
本文介绍了ServiceStack中是否需要ResponseStatus?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要ResponseStatus吗?



维基说,我们需要在我们的响应DTO中有一个ResponseStatus属性来处理异常序列化:



https://github.com/ServiceStack/ServiceStack/wiki/Validation



但是,即使响应DTO中没有ResponseStatus属性,ResponseStatus也会自动生成。



需要ResponseStatus属性?

解决方案

错误处理文档说明如何控制哪些服务返回填充的 ResponseStatus DTO,即:



错误响应类型



抛出异常时返回的错误响应因常规命名的 { RequestDto}响应 DTO存在或n OT。



如果存在:

返回 {RequestDto}回复服务方式的响应类型。如果 {RequestDto} Response DTO有一个 ResponseStatus 属性,则填充,否则将返回 ResponseStatus 。 (如果您已经使用 [DataContract] / [DataMember] 属性修饰了 {ResponseDto} Response 类和属性,则 ResponseStatus 也需要进行装饰,以便填充)。



否则,如果没有:

一个通用的 ErrorResponse 将返回一个填充 ResponseStatus 属性。



服务客户端透明地处理不同的错误响应类型,以及无模式的格式,如JSON / JSV / etc在自定义或通用的 ErrorResponse 中返回 ResponseStatus 之间没有明显的区别,因为它们都在线上输出相同的响应。 >

自定义异常



最终所有的ServiceStack WebServiceExceptions只是响应DTO的一个填充 ResponseStatus 。有许多不同的方法来自定义异常返回,包括:



启用StackTraces



默认显示StackTraces在响应DTO仅在Debug构建中启用,但这种行为是可以覆盖的:



csharp
SetConfig(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 Types

The 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 Exceptions

Ultimately 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 StackTraces

By default displaying StackTraces in Response DTOs are only enabled in Debug builds, although this behavior is overridable with:

csharp SetConfig(new HostConfig { DebugMode = true });

这篇关于ServiceStack中是否需要ResponseStatus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆