Tuesday, March 25, 2008

Implementing WCF Service Part 5 - Fault Contract

In WCF, there are five major contracts – Service Contract, Operation Contract, Data Contract, Fault Contract and Message Contract. In my previous examples I used first three. In this example, I will implement the Fault Contract. I am not very fond of using Message Contract as some guidelines suggest using the least or based on requirement, however you cannot get away from the first four contracts. In the event of Fault (exception), WCF throws a fault containing detail error message which you may not like to be passed back to the caller. In other words, this example focuses on how to hide full error details and provide a custom error code, message and details. You can use multiple Fault Contracts on one Operation Contract in similar way. This example provides a simple and generic way to handle Faults.

In order to show this example, I will add or modify following classes.

1.       ErrorResponse Class - New 
<DataContract(Name:="ErrorResponse", Namespace:="http://http://schemas.vishwamohan.net/2008/03/ErrorResponse")> _   
    Public Class ErrorResponse   
 #Region "Public Fields"   
    <DataMember(Name:="Code", Order:=0)> _   
  Public Code As String = "Error"   
  <DataMember(Name:="Message", Order:=1)> _   
   Public Message As String = "Sorry! An exception occured."   
  <DataMember(Name:="Details", Order:=2)> _   
  Public Details As String = "Please contact support."  
 
 #End Region  
#Region "Constructors"  
     Public Sub New()  
    End Sub  
 
  Public Sub New(ByVal code As String, ByVal message As String, ByVal details As String)  
         Me.Code = code  
         Me.Message = message  
         Me.Details = details  
  End Sub  
#End Region  
 
End Class
 
2.       ICustomerService.vb Class – Modifying Existing Operation Contract, by adding Fault Contract
<FaultContract(GetType(ErrorResponse))> _   
<OperationContract(Name:="GetCustomer")> _     
Function GetCustomer(ByVal ID As Integer) As Customer
 
3.       CustomerService.svc – Modifying Existing Service Contract, by Handling the Exception and Throwing Fault
 
Public Function GetCustomer(ByVal ID As Integer) As Customer Implements ICustomerService.GetCustomer   
              Try   
                  Return ServiceHelper.GetCustomerData(ID)   
              Catch ex As Exception   
                 Throw New FaultException(Of ErrorResponse)(New ErrorResponse, ex.Message)   
                  'or  
         'Throw New FaultException(Of ErrorResponse)(New ErrorResponse("ErrorCode", "ErrorMessage", "ErrorDetails"), ex.Message) 
               End Try   
End Function
 
Note: The value of for reason which is provided by ex.Message is optional, but if you will not provide, WCF Service will generate a message that creator of this fault did not specify the reason. So it will be advisable to pass a proper message in reason.