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

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

问题描述

时ResponseStatus需要的?

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

<一个href=\"https://github.com/ServiceStack/ServiceStack/wiki/Validation\">https://github.com/ServiceStack/ServiceStack/wiki/Validation

然而,它看起来像即使存在在响应没有ResponseStatus属性ResponseStatus是自动生成的DTO

我们是否需要ResponseStatus财产?


解决方案

ServiceStack新的API不再有此限制

编辑:现在我们已经发布了为ServiceStack 新的API设计,不再有此限制。现在您可以自由返回任何你想要的反应类型和C#的客户将继续保留结构化异常处理。

所以,现在的答案是


  

时ResponseStatus需要的?


没有。使用新的API :)当不



使用ServiceStack的旧API时建议:

v3.9.15之前+ 我们的建议是,总是包括它时,你想要结构性错误在客户端上处理可用。如果您要输入错误处理在客户端上你会需要它。

的<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ServiceInterface.ServiceModel/ResponseStatus.cs\">ResponseStatus属性是抱着服务异常和验证错误在 ServiceStack 的占位符。这就是建成ServiceStack所有验证和错误处理序列化到。

即。这是从抛出你的服务里的任何C#的异常被序列化到的<一个实例href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ServiceInterface.ServiceModel/ResponseStatus.cs\">ResponseStatus键入,然后注入到你的回应DTO的 ResponseStatus 属性。

如果你想自动处理和结构化的验证错误,那么你想:


  • 名字您的反应DTO {} RequestDtoName响应,例如客户/ CustomersResponse

  • 在你的回应DTO添加公共ResponseStatus财产。

    • (见<一href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ServiceInterface.ServiceModel/IHasResponseStatus.cs#L8\">IHasResponseStatus接口的ResponseStatus属性的名称和类型应该是什么为例)


您也可以注入你的服务异常的服务器堆栈跟踪,如果你用调用setConfig启用它在你的APPHOST(新EndpointHostConfig {DebugMode =真});

什么ServiceStack返回错误,并在


  

    

这不是自动生成的,没有code根在ServiceStack。


  

否code根在ServiceStack ,唯一的DTO,一个ServiceStack服务返回的是你自己。

随着<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.Endpoints.Tests/ExceptionHandlingTests.cs#L199\">ExceptionHandlingTests展会上,当你的服务在你的HTTP响应返回填充错误唯一的一次是当您的响应DTO包含 ResponseStatus 属性。

为了举例说明,如果您从抛出此自定义异常里面您的服务:

 抛出新CustomException(用户定义的错误);

如果您在您的DTO有一个ResponseStatus你的HTTP响应将包含:

  {responseStatus:{错误code:CustomException
    消息:用户定义的错误,错误:[]}}

如果你没有一个ResponseStatus属性将返回一个空的序列化的DTO,例如

  {}

和最后,如果你没有一个响应DTO通常命名为 {} RequestDto响应就没有身体的。

如果异常是从服务扔到外面发生什么事

ServiceStack确实尝试<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/WebHost.EndPoints/Extensions/IHtt$p$psponseExtensions.cs#L316\">mimic任何未捕获的异常的,如果你只实施 IService&LT; T&GT; (即不是 ServiceBase&LT; T&GT; RestServiceBase&LT; T&GT; )或任何异常发生的之外的服务(例如,通过发射请求绑定,请求/响应滤波器等)什么的看起来的序列化的DTO的XML,JSON和JSON内容类型。

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?

解决方案

ServiceStack's new API no longer has this restriction

Edit: We've now release a new API Design for ServiceStack which no longer has this restriction. You're now free to return any Response type you wish and your C# clients will continue to retain structured Exception handling.

So the answer now is

Is ResponseStatus needed?

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

It is not generated automatically and there is no code-gen in ServiceStack.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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