Understanding Fault Contract in WCF

By | July 27, 2014

In previous WCF tutorials, we learnt about different WCF Contracts including Behavioral (Service Contract, Operation Contract) and Structural (Data Contract and Message Contract). In this post, we will learn about another behavioral Contract i.e. Fault Contract.

What is a Fault Contract?

In WCF (Windows Communication Foundation), we will not expose exception directly to client if it occurs at service level. There is a valid reason for that, “WCF exceptions are basically CLR exceptions containing internal details of service code (e.g. stack trace etc), so it doesn’t make sense to expose it outside CLR“.
In Windows Communication Foundation, Fault Contract is used to return error details to the other party i.e. client. Fault Contract is a behavioral contract that contains the details of possible exception(s) that might occur in a service code.

In order to fully understand the idea, let’s create a WCF Service that generate exception.

[ServiceContract]
public interface ISimpleService
{
               [OperationContract]
               string SimpleOperation();
}
public class SimpleService : ISimpleService
{
              public string SimpleOperation()
             {
                  //Some code here…
                  throw new Exception(“Exception occurred at service level : SimpleOperation error”);
             }
}

While consuming this service on client side, we are expecting above given exception message “Exception occurred at service level : SimpleOperation error” when SimpleOperation is called. But we will get the following error in all cases.

“The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults(either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs”

So, in order to avoid the above generic error and get a customized error message, we will modify our service code as follows:

public string SimpleOperation()
{
          //Some code here…
          throw new FaultException(“Exception occurred at service level : SimpleOperation error”);
}

Now, if we run the client application and call the WCF Service, we will get a proper meaningful error as:

Exception occurred at service level : SimpleOperation error

WCF also facilitates us to use more professional approach by creating our own Custom Exception type and return using Fault Contract. For example, we can create our custom type say “CustomFaultDetail” as follows:

[DataContract]
public class CustomFaultDetails
{
            [DataMember]
            public string ErrorID { get; set; }
           
            [DataMember]
            public string ErrorDetails { get; set; }
}

Note: Custom fault type is attributed as a Data Contract. We have already discussed about Data Contracts in WCF in previous post.

So, above contract and implementation code will be updated accordingly.

[ServiceContract]
public interface ISimpleService
{
               [OperationContract]                [FaultContract(typeof(CustomFaultDetails))]
               string SimpleOperation();
}
public class SimpleService : ISimpleService
{
              public string SimpleOperation()
             {
                  Try{                         
                                      //Do something……                          
                    }catch()
                   {
                            CustomFaultDetails ex = new CustomFaultDetails();
                            ex.ErrorID = “12345“;
                            ex.ErrorDetails = “Specific error details here.“;
                            throw new FaultException(ex,“Reason: Testing…..“);
                  }

             }
}

In this way we can pass more meaningful details about the error to client.

Now, we have covered all types of contracts in WCF including Behavioral(Service Contract, Operation Contract, Fault Contract) as well as Structural (Data Contract and Message Contract).

Other WCF and related Tutorials:

Category: Uncategorized

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.