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 1:36:18 PM #

biodental

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

biodental United States

3/18/2010 1:01:22 PM #

Major Heinle

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...

Major Heinle United States

5/13/2010 3:24:29 AM #

Olimpia Chaidez

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.

Olimpia Chaidez United States

6/9/2010 11:01:22 PM #

Jami Painters

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

Jami Painters United States

9/2/2010 7:21:15 AM #

Ninel Conde Desnuda

Funny, I was thinking the same thing. The post was great btw, can't take credit away from that.

Ninel Conde Desnuda 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

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent Comments

Comment RSS

Live Traffic Feed