Skip to main content

Using Typed and Untyped Faults in WCF Service

Posted in

Typed faults enable you to define custom data types to represent specific errors that occur in your WCF service. Typed faults provide a more structured way of storing information about a specific error than untyped faults, which provide a generic fault placeholder that you can populate with your own meaningful data.

WCF enables you to create typed and untyped faults:

  • Untyped. You use the FaultException class to create an untyped fault. You can set the elements of a SOAP fault, such as Code, Reason, and Detail, through the FaultException constructor. If your client is written by using the .NET Framework, a matching FaultException is thrown on the client side.
  • Typed. You use the generic FaultException<T> type to create a typed fault. In this case, you define a data structure to contain the information about the error and pass an instance of this data type to the FaultException<T> constructor. If your client is written by using the .NET Framework, your defined data type is thrown as a FaultException<T> exception on the client side.

If you choose to use a typed fault, you will need to define a data contract for the type that holds the error information.

To use a typed fault, create an instance of the data type that holds the error information in the service code and pass this instance into the constructor of a generic FaultException. You then throw this populated FaultException to propagate it back to the caller. The WCF service host handles the FaultException and converts it into a SOAP fault

If you choose to use a typed fault, you will need to define a data contract for the type that holds the error information. The following is an example of this type of data contract.

[Visual Basic]

<DataContract([Namespace] := "http://myuri.org/Simple")> _

Public Class AccountOverdrawnFault

    Private m_accountNo As String

   

    <DataMember()> _

    Public Property AccountNo() As String

        Get

            Return m_accountNo

        End Get

        Set

            m_accountNo = value

        End Set

    End Property

    ...

End Class

 

 

[Visual C#]

[DataContract(Namespace="http://myuri.org/Simple")]

public class AccountOverdrawnFault

{   

    private string accountNo;

 

    [DataMember]

    public string AccountNo

    {

        get { return accountNo; }

        set { accountNo = value; }

    }

    ...

}

 

The level of detail in the error is up to you. Depending on the sophistication of the system, you can define error codes and detail structures to be unpacked on the client; however, sometimes just using the Reason string might be sufficient.

To use a typed fault, create an instance of the data type that holds the error information in the service code and pass this instance into the constructor of a generic FaultException. You then throw this populated FaultException to propagate it back to the caller, as shown in the following code.

 [Visual Basic]

Private Sub Withdraw(ByVal account As String, ByVal amount As Decimal)

    Try

           

        bankBusinessObject.Withdraw(account, amount)

        ...

    Catch ex As AccountOverdrawnException

        Dim fault As _

            New AccountOverdrawnFault(ex.accountNo, _

            ex.balanceBefore, ex.amount)

        Throw New FaultException(Of AccountOverdrawnFault)(fault)

    End Try

End Sub

 

 

[Visual C#]

void Withdraw(string account, decimal amount)

{

    try

    {

        bankBusinessObject.Withdraw(account, amount);

        ...

    }

    catch (AccountOverdrawnException ex)

    {

        AccountOverdrawnFault fault =

            new AccountOverdrawnFault(ex.accountNo,

            ex.balanceBefore, ex.amount);

        throw new FaultException<AccountOverdrawnFault>(fault);

    }

}

 

The WCF service host handles the FaultException and converts it into a SOAP fault

For more information about defining faults in WCF, see Defining and Specifying Faults. For more information about using typed faults, see FaultException(TDetail) Generic Class