何时使用 WCF 故障异常
我有一个简单的 WCF 服务,它执行一个简单的操作:
[OperationContract]
DoSomething (Stuff input);
如果 DoSomething
内部发生异常,则将返回 FaultException
。鉴于客户端需要知道的只是是否出了问题,您是否认为在这种情况下不需要定义 FaultException
?
I have a simple WCF service that carries out a simple operation:
[OperationContract]
DoSomething (Stuff input);
If an exception occurs inside DoSomething
then a FaultException
will be returned. Given that all the client needs to know is whether something went wrong, would you say that there's no need to define a FaultException
in this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回
FaultException
始终是一个好习惯,因为如果不这样做,通道将出现故障并且无法再次使用。需要向客户端发送哪些信息的决定是在配置中(在服务行为中)进行的:
事实上,我总是实现
IErrorHandler
服务上的行为以捕获所有异常并返回FaultException
以便我不必在我的所有操作中都这样做。It is always good practice to return a
FaultException
, since if you do not, the channel will be faulted and cannot be used again.The decision what information need to be sent to the client is taken in the configuration (in service behaviour):
In fact I always implement
IErrorHandler
behaviour on the service to catch all exceptions and returnFaultException<T>
so that I do not have to do it in all my operations.