WCF Fault Contracts - Day 2

Introduction

This article explains fault contracts in Windows Communication Foundation. It also explains the difference between faults & exceptions and provides sample code for implementing fault contracts.

I highly recommend reading WCF Introduction and Contracts - Day 1 before this article.

Fault Contracts

Windows Communication Foundation enables us to specify the fault behavior of our service.

For example

We create a service and this service is consumed by a client. Suppose this service throws an exception. But how can the client be notified of this exception? Because whenever an exception is thrown from a service, it cannot be sent to the client. But if you want to know the reason then a fault contract can help us. We can send information regarding this with the fault.

Faults Vs. Exceptions

The main thing in faults and exceptions is that "faults and exceptions are not the same thing".
An exception is a .Net mechanism. It is used when the program encounters an error. The .Net language allows us to throw, catch and ignore the exception. At some point they should be handled or the .Net runtime will terminate the thread in which the exception was thrown.
Faults refer to the SOAP fault mechanism to transfer error information from a service to a client. WCF provides the FaultException class. Whenever a service throws a FaultException, WCF serializes the information sent to the client.

FaultException

A FaultException is used to send untyped fault data to the client.

FaultException<TDetail>

This generic version is used to send typed fault data to the client. TDetail represents the type parameter for the fault information. Now let's see the example of typed fault data.
Create one WCF Service Application. Add the following segment of code in the Interface. In this code we are using the FaultContract attribute. It is applied to the interface only. Suppose we want to return various types of faults then we need to set the FaultContract attribute multiple times.

    [ServiceContract]

    public interface IService1

    { 

        [OperationContract]

        [FaultContract(typeof(FaultInfo))]

        string Topup(string Operator, string MobileNumber, double Amount);

    } 

    [DataContract()]

    public class FaultInfo

    {

        [DataMember()]

        public string Reason = null;

    }

If we choose to use a typed fault then we need to define a DataContract for the type that holds the error information. In our example we create a datacontract for the FaultInfo class and we create one data member i.e. reason to hold the error information.
Add the following lines of code in the .svc.cs file. In this method we check whether the amount is double or not. If the amount is double then we throw a fault exception.

public string Topup(string Operator, string MobileNumber, double Amount)

    {

        if (Amount == 0.0d)

        {

            FaultInfo fi = new FaultInfo();

            fi.Reason = "Amount should not be in double.";

            throw new FaultException<FaultInfo>(fi,new FaultReason(fi.Reason));

        }

        return "Recharge Successful.";

    }

If the amount is double then we create an object of the "FaultInfo" class and set the information we want to send to the client in the datamember i.e. the Reason. Finally we throw a fault exception of the fault info type.

Conclusion

This article explains FaultException and the difference between faults and exceptions. It also explains the sample code to implement fault exceptions. You can download the code and check the application.

Don't forget to read WCF Introduction and Contracts - Day 1

Up Next
    Ebook Download
    View all
    Learn
    View all