Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF catching Fault Exceptions of type T or Base type

We have a system with a WCF layer.

The WCF services can throw various FaultExceptions, these are exceptions of type:

FaultException<MyStronglyTypedException>

All strongly types exceptions inherit from a base exception.

public class MyStronglyTypedException : MyBaseException

I can catch FaultException, but then I do not have access to the Detail property of FaultException.

What I would like to catch is:

FaultException<MyBaseException>

But this does not seem to be possible.

Is there a way that I can get access to the Detail property of the FaultException, without catching every individual strongly typed exception?

like image 461
Shiraz Bhaiji Avatar asked Feb 08 '10 10:02

Shiraz Bhaiji


People also ask

Which type of exception can be thrown from WCF service?

Fault exceptions are exceptions that are thrown by a WCF service when an exception occurs at runtime -- such exceptions are typically used to transmit untyped fault data to the service consumers.

Which type of behavior should be configured to return exception detail from WCF service?

We can use FaultException &/or FaultContract (in System. ServiceModel namespace) to these exception details to the client.

How do you handle a fault exception in C#?

In a service, use the FaultException class to create an untyped fault to return to the client for debugging purposes. In a client, catch FaultException objects to handle unknown or generic faults, such as those returned by a service with the IncludeExceptionDetailInFaults property set to true .


1 Answers

If you want to be able to catch the strongly typed FaultException<MyBaseException> in your client code, you must decorate your service method with the FaultContract attribute for that type:

[ServiceContract]
interface IYourService
{
   [OperationContract]
   [FaultContract(typeof(MyBaseException))]
   ResponseType DoSomethingUsefulHere(RequestType request);
}

If you don't "declare" those specific types which you want to catch strongly typed FaultContract<T> exceptions for, WCF will convert all server side faults into general-purpose FaultContract instances.

like image 118
marc_s Avatar answered Oct 21 '22 06:10

marc_s