Learning Video on Fault Contract in WCF

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();

        }

    }

}

 

5 responses to “Learning Video on Fault Contract in WCF”

  1. Good work… Thanks you…

  2. […] FAULT Contract in WCF with Learning Video Video of the article is HERE […]

  3. Dhananjay, Nice Video and I found it on time when looking in to WCF!
    1001 Congrests!

  4. 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 ?

  5. Great explaination… thx

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 )

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com