Web 服务是否应该抛出异常或结果对象 [英] Should web services throw exceptions OR result objects

查看:28
本文介绍了Web 服务是否应该抛出异常或结果对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在 Web 服务中抛出异常是个好主意,我不确定我是否完全满意.如果不是堆栈跟踪,我不会介意.这不是我想要的.

I'm not sure I'm completely happy that throwing exceptions in web services is a good idea. I wouldnt mind as much if it wasn't for the stack trace. This is not something I wan't.

我研究了几种实现方式,但似乎并没有就此达成共识.例如,CampaignMonitor 确实会返回一个 Result 对象,但其他人则不会.

I have researched around several implementations and there really doesn't seem to be a consensus on this. CampaignMonitor for example does return a Result object, yet others don't.

从架构上来说,我不确定返回一个返回对象是否有意义,当然异常是一个异常,但我喜欢返回对象的地方在于它对最终用户来说是一个更优雅的解决方案.

Architecturally, I'm not sure returning a return object makes sense, surely an exception is an exception, but what I do like about a Return object is that it is a more graceful solution for the end user.

有人有更好的解决方案吗?

Does anyone have any better solutions?

编辑

顺便说一句,我正在使用 ASMX 网络服务,其中不能选择打开 CustomErrors.

BTW I am using ASMX web services, where turning CustomErrors on is not an option.

推荐答案

你在说什么堆栈跟踪?你试过这个吗?

What stack trace are you talking about? Have you tried this?

在 ASMX 和 WCF 服务中,未捕获的异常将转换为 SOAP 故障.在这两种情况下,它们都可以配置为不包含任何堆栈跟踪.事实上,这是 WCF 中的默认设置.

In both ASMX and WCF services, an uncaught exception will be translated into a SOAP Fault. In both cases, they can be configured to not include any stack trace. In fact, that's the default in WCF.

因此,返回这样的错误的正确方法是通过错误.产生错误的一种方法是抛出而不处理异常.

So, the proper way to return an error like this is through a fault. One way to generate faults is to throw and not handle an exception.

这篇关于Web 服务是否应该抛出异常或结果对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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