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
   1:  <DataContract(Name:="ErrorResponse", Namespace:="http://http://schemas.vishwamohan.net/2008/03/ErrorResponse")> _
   2:  Public Class ErrorResponse
   3:  #Region "Public Fields"
   4:      <DataMember(Name:="Code", Order:=0)> _
   5:      Public Code As String = "Error"
   6:      <DataMember(Name:="Message", Order:=1)> _
   7:      Public Message As String = "Sorry! An exception occured."
   8:      <DataMember(Name:="Details", Order:=2)> _
   9:      Public Details As String = "Please contact support."
  10:   
  11:  #End Region
  12:   
  13:  #Region "Constructors"
  14:      Public Sub New()
  15:   
  16:      End Sub
  17:   
  18:      Public Sub New(ByVal code As String, ByVal message As String, ByVal details As String)
  19:          Me.Code = code
  20:          Me.Message = message
  21:          Me.Details = details
  22:      End Sub
  23:  #End Region
  24:  End Class
 
2.       ICustomerService.vb Class – Modifying Existing Operation Contract, by adding Fault Contract
   1:  <OperationContract(Name:="GetCustomer")> _
   2:          <FaultContract(GetType(ErrorResponse))> _
   3:          Function GetCustomer(ByVal ID As Integer) As Customer
 
3.       CustomerService.svc – Modifying Existing Service Contract, by Handling the Exception and Throwing Fault
 
   1:  Public Function GetCustomer(ByVal ID As Integer) As Customer Implements ICustomerService.GetCustomer
   2:              Try
   3:                  Return ServiceHelper.GetCustomerData(ID)
   4:              Catch ex As Exception
   5:                  Throw New FaultException(Of ErrorResponse)(New ErrorResponse, ex.Message)
   6:                  'or
   7:                  'Throw New FaultException(Of ErrorResponse)(New ErrorResponse("ErrorCode", "ErrorMessage", "ErrorDetails"), ex.Message)
   8:              End Try
   9:  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.
Signature

Comments

8/3/2009 11:36:18 PM #

biodental

Great job mate! I like your work. Thanks for sharing. The blogengine is new to me

biodental United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



About Me

Me Hello,my name is Vishwa Mohan Kumar.
I am a Software Architect. This blog is result of my experiments.

Flickr Photos

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Recent Comments

Comment RSS

Live Traffic Feed