Introduction

What is the use of FaultContract?

In simple WCF Service errors/Exceptions can be passed to the Client (WCF Service Consumer) by using FaultContract.

How do you handle errors in ASP.NET? It's very simple - just by adding the simple Try & Catch blocks. But when you come to WCF Service, if any unexpected error occurred (like SQL server down/Unavailability of data/Divide By Zero) in service, then error/Exception details can be passed to Client by using Fault Contract.

I am giving extensive training's on this topic and all other .Net related training's like ASP.NET, C#.NET, ASP.NET MVC, WCF, WPF & SQL Server. I request you to visit my website here http://onlinedotnet.com

Once you are done reading this article, please go through the Fault Contract Interview questions and answers here.

False Assumption

Most people think that we can't throw FaultException in catch block. It should be only based on some condition (if condition). But it is a false assumption. The main objective is, any type of exceptions(Predicted or Unpredicted) from service to be passed to Client (WCF Service consumer).

Using the Code

This is a very simple WCF service implementation to understand the usage of the FaultContract. Here I am implementing the TestConnection() method
in the WCF service. This method tries to open some SQL Server connection. If any errors occur while opening the connection, it throws the error details to the client
by using the Fault Contract.

Here my solution contains 2 projects:

Service Implementation in ASP.NET

Consuming the service in Console application

Note:
This article is not written to test the connection string. This is to understand
the usage of FaultContract. So I have taken this basic example to explain to you better.

1. Service Implementation

Create a WCF Service application project and implement the service with the following code.

The TestConnection() method added with FaultContract attribute in IService1 interface.
It means that service errors should be passed to the client with the type of ServiceData class.

ServiceData class is DataContract class. The Error & Success message details to be added to this data members.

Here I am implementing the IService1 interface. This interface contains only one method TestConnection() with one input
parameter StrConnectionString. For this input parameter SQL Server connection details should be passed from the client side.

Note: This example is only to understand the basic use of Fault Contract. This article doesn't concentrate on service security.
You can try this article from your localhost SQL connection by passing valid & invalid connection string details.

2. Consuming the Service in Console Application

Create a new Console project and give the above service reference. Here, we are creating an object to the Service1Client class
and calling the TestConnection() method by passing the connection string. If connection succeeds, then it shows "Connection Succeeded" message.
If unable to open connection, then it moves to the catch block and displays the appropriate error.

Summary

Hope this article is useful to understand the usage of the FaultContract. This article is focused on how to implement a basic WCF service and how to handle the errors.
Please don't forget to rate it if you like it.

Comments and Discussions

Thanks! this helped me get Fault Exceptions working. I've still had some problems with JSON formatted exceptions though, it seems to be fairly easy to get those to serialize from the WCF Service by disabling fault exceptions in the endpoint behaviour end but deserializing them from the client side I couldn't get to work. Sticking with xml for now.

Nice article, I'm looking for a way to be able to log (my custom logger) infrastructure exceptions i.e. occurring before the web method is called. I merely want to log them and let the default behavior to handle the rest.

I am Sanjeev Jyothikrishnan, a .Net Developer(Fresher). I am learning WCF services and I went through Fault contract posted by you. I want to ask a doubt that in the below piece of code under catch (SqlException sqlEx), myServiceData.Result should be "TRUE" or "False"?? What you gave is "TRUE" but I am confused. Please do reply me. Please excuse me if I am wrong.