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 num1int 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 num1int 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(1155);
                Console.WriteLine(result);
                Console.ReadLine();
            }
            catch (FaultException<CustomExceptionex)
            {
                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

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives