WCF – Exception Handling, FaultException,FaultException and FaultContracts

Whenever we write a program we should expect the unexpected and add appropriate code to handle those exceptions.When we write a windows based application we use try/catch blocks in our methods to catch exceptions, and also we show a user friendly message in UI.

But when we work with WCF Service, It is not that much straight forward, So

  • How do we handle exceptions in WCF Service?
  • How to propagate a user-friendly error message to the client, who consumes the WCF Service?
  • How to send business rule validation messages to WCF client?

Handling exceptions in WCF Service is different than usual exceptions in .NET. As the WCF clients may not be based on .NET Technologies we cannot utilize the .NET Exception object to exchange exception details over the wire. For this purpose we should have a generic way to pass exception details to all type of clients, who consumes WCF Service.

For this reason we have SOAP faults, SOAP faults are the way to propagate the exceptions from the service to the client application. .Net has FaultException<T> generic class which can raise SoapFault exception. Service should throw FaultException<T> instead of usual CLR Exception object. T can be any type which can be serialized.

Read more form CodeProject.com

WCF FaultException Caught

Advertisements

2 thoughts on “WCF – Exception Handling, FaultException,FaultException and FaultContracts

  1. http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue
    https://devzone.channeladam.com/articles/2014/07/how-to-call-wcf-service-properly/
    https://devzone.channeladam.com/articles/2014/09/how-to-easily-call-wcf-service-properly/
    http://dzimchuk.net/post/wcf-error-helpers

    https://github.com/JayDouglass/WcfProxy/blob/master/WcfProxy.Tests/WcfProxyTests.cs

    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?

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s