Welcome to Vishwa's blog - Technology, Spirituality and More...

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://")> _   
    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   
                  Return ServiceHelper.GetCustomerData(ID)   
              Catch ex As Exception   
                 Throw New FaultException(Of ErrorResponse)(New ErrorResponse, ex.Message)   
         '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.

Comments (4) -

  • biodental

    8/3/2009 1:36:18 PM | Reply

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

  • Major Heinle

    3/18/2010 1:01:22 PM | Reply

    First of all great post I just wanted to ask on what is the difference between blogenenigne and wordpress blogs? Is it easier to use or more efficient? I amseeing a lot of blogs powered by this software popping up lately and wondering if it is better or not? Thanks...

  • Olimpia Chaidez

    5/13/2010 3:24:29 AM | Reply

    You have actually created some excellent points here. I specifically appreciate the way you've been able to stick so much thought into a relatively short post (comparitively) which creates it an thoughtful publish on your subject. In my opinion, you've presented the topic in a quite thorough yet concise manner, that is genuinely useful when someone wants to get the facts without spending too a lot time searching the Internet and sifting out the noise to discover the answers to their questions. I usually get so frustrated with so numerous in the final results inside the major SE's due to the fact they normally seem to mostly be filled with filler content that often isn't extremely sensible. If you don't mind I'm going to add this post and your blog to my delicious favorites so I can share it with my family. I appear forward to coming back to read your future posts as well.

  • Jami Painters

    6/9/2010 11:01:22 PM | Reply

    I've this a post of yours before and I really like it!