This video is giving basic explanation on How to work with Fault Contract.

For reference Soure code is as below ,

IService1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace demoFaultService

{

 

    [ServiceContract]

    public interface IService1

    {

        [OperationContract ]

        [FaultContract(typeof(DivideByZeroException))]

        double Div (double number1, double number2);

 

    }

}

 

Service.svc.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace demoFaultService

{

 

    public class Service1 : IService1

    {

        public double Div(double number1, double number2)

        {

            if(number2 ==0)

            {

            DivideByZeroException exception = new DivideByZeroException();

                throw new FaultException<DivideByZeroException>(exception,“Number 2 is Zero at Service Side”);

            }

 

            return number1 % number2;

        }

 

 

    }

}

 

Client code is as below

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using ConsoleApplication3.ServiceReference1;

 

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            Service1Client proxy = new Service1Client();

            try

            {

                double result = proxy.Div(30, 0);

                Console.WriteLine(result);

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadKey();

        }

    }

}

 

Advertisements

5 thoughts on “Learning Video on Fault Contract in WCF

  1. Good work y Dhananjay Kumar. It really helps to me to understand what the FaultException is.

    Here I have one question,

    Even if I am not using [FaultContract(typeof(DivideByZeroException))], Then also error is propagating to client. then what is the use of fault contract ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s