简体   繁体   English

Wcf异常处理

[英]Wcf exception handling

I noticed that if you do a throw new InvalidCastException for example, the channel state on the client side is faulted. 我注意到,例如,如果您执行抛出新的InvalidCastException,则客户端上的通道状态出现故障。 But if you throw new FaultException, the channel state on the client side is opened. 但是如果抛出新的FaultException,则会打开客户端的通道状态。

By curiosity, what is the reason why one faults the channel and the other doesn't? 由于好奇心,人们为什么会对通道进行故障而对另一方没有故障的原因是什么?

The FaultException is a special case in WCF. FaultException是WCF中的一个特例。 It's meant to indicate that something happened on the service side that was an error, but at the same time, not fault the channel. 这意味着表明服务端发生的某些事情是错误的,但与此同时,并不是通道的错误。 This makes sense, given you can embed this information into the contract using the FaultContractAttribute to expose what can be expected from a contract. 这是有道理的,因为您可以使用FaultContractAttribute将此信息嵌入到合同中,以揭示合同中可能出现的情况。

Other exceptions are not really translatable in the WS world. 其他例外在WS世界中并不是真正可以翻译的。 Exceptions are a technology-specific abstraction, and each technology stack has a different representation of that abstraction (or in some cases, none at all). 例外是一种特定于技术的抽象,每个技术堆栈都有不同的抽象表示(或者在某些情况下,根本没有抽象)。

That being said, when an exception that is not a fault exception is thrown on the server side, it is seen as catastrophic by the WCF runtime, and the channel must be faulted, as it is not known if you can proceed or not. 话虽这么说,当在服务器端抛出故障异常的异常时,它被WCF运行时视为灾难性的,并且通道必须出现故障,因为不知道您是否可以继续。

However, using FaultException, it implies you have some foresight into the conditions around why it was thrown and whether or not the underlying channel has been impacted or not. 但是,使用FaultException,它意味着您对抛出它的原因以及底层通道是否受到影响有一些先见之明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM