Wednesday, 23 October 2013

Catching WCF FaultException in ASP.NET Web Application

If you are making WCF method calls from your app it will be handy if your code give error details when something goes wrong with the WCF call. This can be done by adding a catch(System.ServiceModel.FaultException){} block within your code.
ServiceClient serviceClient;

try
{

//create ServiceClient 

//Service method call

}
catch (System.ServiceModel.FaultException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
finally
{
if (serviceClient != null)
serviceClient.close()
}
With this code you can put a break point within the catch(System.ServiceModel.FaultException ex) block and inspect the fault exception as seen in the image below.

1 comment:


  1. any good patterns and practices about FaultExceptions and Wcf Error Handling ?

    any good patterns and practices about it ?

    An example of handling errors at the client:



    try

    {

    proxy.SomeOperation();

    }

    catch (FaultException ex)

    {

    // only if a fault contract was specified

    }

    catch (FaultException ex)

    {

    // any other faults

    }

    catch (CommunicationException ex)

    {

    // any communication errors?

    }

    ReplyDelete