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.
public interface ISimpleService
{
[OperationContract]
string SimpleOperation();
}
{
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.
So, in order to avoid the above generic error and get a customized error message, we will modify our service code as follows:
{
//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:
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:
public class CustomFaultDetails
{
public string ErrorID { get; set; }
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.
public interface ISimpleService
{
[OperationContract] [FaultContract(typeof(CustomFaultDetails))]
string SimpleOperation();
}
{
public string SimpleOperation()
{
Try{
{
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:
- WCF Hosting (Console | Windows Service | IIS | WAS )
- WCF Tutorial Step by Step
- 5 simple steps to create your first WCF RESTful Service
- WCF Vs ASMX Service
- MVC3 Vs MVC4 Vs MVC5
- Difference betweeen ASP.NET WebForms and ASP.NET MVC
- 3 ways to generate proxy for WCF Service
- Post JSON data to WCF RESTful Service using jQuery
- Practical Guide to ASP.NET Web API