Thursday, 19 September 2013

Fault Contract in WCF

What is fault Contract?

A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client side. The purpose of a Fault Contract is to handle an error by the service class and display in the client side. Whenever the client makes a call to a service class and an unexpected exception comes such as, SQL Server is not responding, or Divide by zero; in this case the service class throws an exception to the client using the Fault Contract. 

By default when we throw an exception from a service, it will not reach the client. WCF provides the option to handle and convey the error message to the client from a service using a SOAP Fault Contract.

To support SOAP Faults, WCF provides the FaultException class. This class has two forms:
  1. FaultException: to send an untyped fault back to the caller
  2. FaultException<T>: to send a typed fault data to the client. It is basically a Generic Type.
Syntax    [OperationContract]
    [FaultContract(typeof(MyException))]
    string getDetails(int value);


The Fault Contract sample demonstrates how to communicate error information from a service to a client. The sample is based on the, with some additional code added to the servie to convert an internal exception to a fault.

Now we are going to implement the Fault Contract .

Create a new Project . add  WCF Application Project. the Code are :

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;
using System.Text;

namespace FaultContractApp
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract (typeof(CustomException))]
        int add(int num1, int num2);
    }

    [DataContract()]
    public class CustomException
    {
        [DataMember()]
        public string Title;

        [DataMember()]
        public string ExceptionMessage;

        [DataMember()]
        public string InnerException;

        [DataMember()]
        public string StackTrace;
    }

 ------------------------------------------------
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace FaultContractApp
{
    public class Service1 : IService1
    {

        public int add(int num1, int num2)
        {
            CustomException _fault = new CustomException();
            _fault.ExceptionMessage = "Exception occured in the service!";
            _fault.InnerException = "Inner EXception";
            _fault.Title = "Exception Title";
            _fault.StackTrace = "Stack Trace message from service.";
            throw new FaultException<CustomException>(_fault,"Reason: Testing the Fault contract");
        }
    }
}

now add a new Console Project in your Solution. add the ServiceReference in your Console Project .
now write the Code to use Service . the Code are :

using System.ServiceModel;
using myConsole.ServiceReference1;

namespace myConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client _service = new Service1Client();
            try
            {
                int result = _service.add(11, 55);
                Console.WriteLine(result);
                Console.ReadLine();
            }
            catch (FaultException<CustomException> ex)
            {
                Console.WriteLine(string.Format("Title ->  {0} ", ex.Detail.Title));
                Console.WriteLine(string.Format("ExceptionMessage ->  {0} ", ex.Detail.ExceptionMessage));
                Console.WriteLine(string.Format("Inner Exception ->  {0} ", ex.Detail.InnerException));
                Console.WriteLine(string.Format("StackTrace ->  {0} ", ex.Detail.StackTrace));
                Console.ReadLine();
            }
        }
    }
}

Now debug the Console Application. you will get the Fault Exception generated by Service end.



the Complete Demo Project is attached: Click me!

0 comments:

Post a Comment

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (9) Web Service (1) XMl (1)