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.

5 comments:

Anonymous said...

Hi Vishwamohan,

I am new to the .net world. I have implemented a WCF client and service which communicates through MSMQ. Now I want the service to forward the messages it receives from the client to an external SOAP service implemented in Java. Any suggestions on how to approach the last part of my puzzle. I will appreciate any help.

Thanks,
Janak.

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...

Genial dispatch and this fill someone in on helped me alot in my college assignement. Gratefulness you on your information.

Anonymous said...

Easily I agree but I contemplate the list inform should secure more info then it has.

Anonymous said...

You're so cool! I do not suppose I have read through anything like this before. So great to find somebody with a few original thoughts on this topic. Seriously.. thanks for starting this up. This site is one thing that's needed on the web, someone with a little originality!
Here is my weblog ... penis extender