问题描述
时ResponseStatus需要的?
维基说,我们需要在我们的反应有一个ResponseStatus属性DTO来处理异常的序列化:
https://github.com/ServiceStack/ServiceStack/wiki/Validation
然而,它看起来像即使存在在响应没有ResponseStatus属性ResponseStatus是自动生成的DTO
我们是否需要ResponseStatus财产?
ServiceStack新的API不再有此限制
编辑:现在我们已经发布了新的API设计,不再有此限制。现在您可以自由返回任何你想要的反应类型和C#的客户将继续保留结构化异常处理。
所以,现在的答案是
No. not when using the new API :)
Recommendations when using ServiceStack's old API:
Before v3.9.15+ our recommendation was to always include it whenever you want structured error handling available on the client. If you want typed error handling on the client you will need it.
The ResponseStatus property is the place holder for holding service exception and validation errors in ServiceStack. It's what all validation and error handling built into ServiceStack serializes into.
i.e. Any C# exceptions that are thrown from inside your service gets serialized into an instance of the ResponseStatus type and injected into the ResponseStatus
property in your Response DTO.
If you want auto-handling and structured validation errors then you want to:
- Name your Response DTO
{RequestDtoName}Response
, e.g. Customers/CustomersResponse - Add a public ResponseStatus property on your Response DTO.
- (See the IHasResponseStatus interface for an example of what the Name and type of the ResponseStatus property should be)
You can also inject the server StackTrace of your services exception if you enable it in your AppHost with SetConfig(new EndpointHostConfig { DebugMode = true });
What does ServiceStack return for errors and when
There is no code-gen in ServiceStack, the only DTO that a ServiceStack service returns is your own.
As the ExceptionHandlingTests show, the only time when your service returns the populated error in your HTTP Response is when your Response DTO contains a ResponseStatus
property.
For illustrative purposes if you threw this custom exception from inside your service:
throw new CustomException("User Defined Error");
If you had a ResponseStatus in your DTO your HTTP Response will contain:
{"responseStatus":{"errorCode":"CustomException",
"message":"User Defined Error","errors":[]}}
If you didn't have a ResponseStatus property it will return an empty serialized DTO, e.g
{}
And lastly if you didn't have a Response DTO conventionally named {RequestDto}Response
there would be no body at all.
What happens when an exception is thrown from outside your service
ServiceStack does try to mimic any uncaught exceptions if you only implement IService<T>
(i.e. Not ServiceBase<T>
or RestServiceBase<T>
) or for any exceptions occurring outside of your service (e.g. Request binding, Request/Response filters, etc) by emitting what looks like a serialized DTO for the XML, JSON and JSON Content types.
这篇关于是ResponseStatus需要ServiceStack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!