Pages

Monday, April 16, 2012

Fault Contract in WCF


Exceptions are technology specific. Hence they cannot cross service boundaries. Soap represent standard for errors.WCF can transfer error from service to client through faults. If the error is a fault Exception then all the details are transferred else only generic fault.
i.e. InternalServiceFault
we can use either [ServiceBehavior] or to enable Fault.
Example:-
<serviceBehaviors>
        <behavior>
         
          <serviceMetadata httpGetEnabled="true"/>
         
          <serviceDebug includeExceptionDetailInFaults="True"/>
        behavior>
      serviceBehaviors>

You can throw a Soap Fault using the FaultException class
You can specify the fault code and reason when you create it. All this details will be traveled to the client side.
Example:-
 Throw New FaultException(Of FaultExceptionClass)(New FaultExceptionClass)
At the client side we can use the try catch block to catch this faultException
Example:- Catch ex As FaultException(Of FaultService.FaultExceptionClass)
            Console.WriteLine("Fault value " + ex.Detail.m_Reason)
            Console.WriteLine("Fault value " + ex.Detail.m_InnerReason)
Example:-


Client Side Code:-
Imports System.ServiceModel
Module Module1
    '''


    ''' Main class
    '''

    '''
    Sub Main()
        Try
            '   Create Service Proxy
            Dim ser As New FaultService.Service1Client
            '   Call the service
            Console.WriteLine(ser.GetData(1))
            Console.ReadLine()
            '   Handle Fault Error
        Catch ex As FaultException(Of FaultService.FaultExceptionClass)
            '   Two Property exposed by the FaultExceptionClass Class.
            Console.WriteLine("Fault value " + ex.Detail.m_Reason)
            Console.WriteLine("Fault value " + ex.Detail.m_InnerReason)
            Console.ReadLine()
            '   Handle all other generic error.
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub

End Module

Server Side Code:-

'''


''' This Class wil have fault property exposed to client.
'''

'''
<DataContract()>
Public Class FaultExceptionClass
    Implements IDisposable

    <DataMember()>
    Private m_Reason As String
    Public Property Reason() As String
        Get
            Return m_Reason
        End Get
        Set(ByVal value As String)
            m_Reason = value
        End Set
    End Property
    <DataMember()>
    Private m_InnerReason As String
    Public Property InnerReason() As String
        Get
            Return m_InnerReason
        End Get
        Set(ByVal value As String)
            m_InnerReason = value
        End Set
    End Property

    '   Service Interface
<ServiceContract()>
Public Interface IService1
    '   use FaultContract type to Specify Fault Type.
    <FaultContract(GetType(FaultExceptionClass))>
    <OperationContract()>
    Function GetData(ByVal value As Integer) As String
End Interface

'   Class that implement IService1 Interface
Public Class Service1
    Implements IService1


    Public Sub New()
    End Sub

    '   Function which throws fault exceptions.
    Public Function GetData(ByVal value As Integer) As String Implements IService1.GetData
        Try
            Throw New FaultException(Of FaultExceptionClass)(New FaultExceptionClass)
        Catch ex As Exception
            Using objError As New FaultExceptionClass
                objError.Reason = "Message"
                objError.InnerReason = "InnerException"
                Throw (New FaultException(Of FaultExceptionClass)(objError, "Data Error"))
            End Using
        End Try
        Return String.Format("You entered: {0}", value)
    End Function
End Class

<_!--Change in WebConfig-->
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <_!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <_!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True"/>
        behavior>
      serviceBehaviors>
    behaviors>









No comments: