Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wcf exception handling

Tags:

c#

.net

wcf

I noticed that if you do a throw new InvalidCastException for example, the channel state on the client side is faulted. But if you throw new FaultException, the channel state on the client side is opened.

By curiosity, what is the reason why one faults the channel and the other doesn't?

like image 719
pdiddy Avatar asked Apr 05 '10 13:04

pdiddy


People also ask

Which of the exception does WCF throw?

Expected exceptions from communication methods on a WCF client include TimeoutException , CommunicationException , and any derived class of CommunicationException . These indicate a problem during communication that can be safely handled by aborting the WCF client and reporting a communication failure.

Which contract in WCF is used for error handling?

Since a client's concern area is not about how an error occurred or the factors contributing to an error, SOAP Fault contract is used to communicate the error message from the service to the client in WCF. A Fault contract enables the client to have a documented view of the errors occurred in a service.

What is difference between communication exception and FaultException in WCF?

They're not same. In a WCF service, if it throws an exception inside the service, the client will not get the details. In order to get the formatted exception details on client side, you need to use FaultException instead to let the client know the details. The FaultException information can be serialized as expected.


1 Answers

The FaultException is a special case in 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.

Other exceptions are not really translatable in the WS world. 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.

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.

like image 158
casperOne Avatar answered Sep 22 '22 16:09

casperOne