ServiceStack:如何处理错误?
到目前为止,我使用 ServiceStack 取得了很好的效果,只是处理错误似乎很棘手。如果在消息序列化期间出现问题(例如,因为我忘记向消息添加默认构造函数),则客户端返回的所有消息都是服务器有内部错误且状态代码为 500 的消息。 Global.asax 中的 HttpApplication.Error 事件不起作用,因为它永远不会被命中。 Application_Error
也没有。这不仅对于最终用户场景来说是不够的,而且还使调试这些错误变得非常麻烦,因为找出问题所在的唯一方法是快速观察中这个丑陋的表达:
Encoding.Default.GetString( ((System.IO.MemoryStream)((SyncMemoryStream)((System.Net.HttpWebResponse)(((WebException)ex).Response)).ResponseStream))._buffer)
我想要的是捕获服务器上的所有错误侧(无论是 ServiceStack 的序列化,还是我的服务上的错误)并将所需信息添加到我所有消息类型都具有的 Errors
集合中。
I'm using ServiceStack with great results so far, except that dealing with errors is seemingly tricky. If something goes wrong during serialization of a message (because I forgot to add a default constructor to the message for example) all the client gets back is a message that the server had an internal error and a status code of 500. Adding a listener to the HttpApplication.Error
event in the Global.asax doesn't work as it never gets hit. Neither does Application_Error
. Not only is this insufficient for end user scenarios, it makes debugging these errors very cumbersome as the only way to find out what went wrong is this ugly expression in the Quick Watch:
Encoding.Default.GetString( ((System.IO.MemoryStream)((SyncMemoryStream)((System.Net.HttpWebResponse)(((WebException)ex).Response)).ResponseStream))._buffer)
What I'd like is catch any and all errors on the server side (be it serialization by ServiceStack, or errors on my services) and add the required information to an Errors
collection that all my message types have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关 ServiceStack 中错误处理和验证的更多详细信息,请参阅 ServiceStack 的验证和错误处理 wiki 页面。
目前无法使用自定义逻辑处理序列化异常(尽管我现在将其添加到 TODO 列表中:)。
如果您的Response DTO具有ResponseStatus属性(即或继承自IHasResponseStatus),ServiceStack应该自动序列化您的异常。
为了让它序列化您的 StackTrace,请使用 AppHost.Configure() onload 脚本中的 SetConfig() 将 DebugMode 设置为 true。
See ServiceStack's Validation and Error handling wiki page for more details about error handling and validation in ServiceStack.
At the moment there is no way to handle a serialization exception with Custom logic (although I will now add that on the TODO list :).
If your Response DTO has a ResponseStatus property (i.e. or inherits from IHasResponseStatus), ServiceStack should automatically serialize your exception.
To also get it to serialize your StackTrace set DebugMode to true with SetConfig() in your AppHost.Configure() onload script.