Video of the article is HERE 

Objective

This article will give basic introduction on

1. How to handle Exception at service side?

2. How to use Fault contract at Service side?

3. How to handle Service Exception at client side?

Video explaining this article is here

Few points about Exception at Service

1. Exception is technology specific.

2. Exception should not share beyond service boundary.

3. Since Exception is technology specific they cannot be propagated to client.

4. Exception are of types

Ø CLR Exception

Ø Windows32 Exception

Ø Runtime Exception at service

Ø C++ Exception

5. Exception are very much native to the technology in which service is made.

6. Exception must be converted from technology specific information to natural information that can be communicated to client.

clip_image002

SOAP Fault

clip_image003

FaultException<T>

1. Service should throw FaultException<T> instead of usual CLR exception.

2. FaultException<T> is specialization of Fault Exception.

3. Any client that programs against FaultException can handle exception thrown by FaultException<T>.

4. The type parameter T conveys the error detail.

5. T can be of any type like Exception, CLR Type or any type that can be serialized. 

6. T can be of type Data contract.  

7. T is a generic parameter conveys the error details.  

 

About Fault Contract

1. Any Exception service want to share beyond service boundary comes must be goes at the part of service contract.

2. Fault contract says what type of exception service can throw to client.

3. The type in Fault Contract must be the same as the T of FaultException<T>

4. Fault contract is the contact between service and client about what exception can be thrown from service to client.

5. Fault contract is attributing of a method in the service. So only attributed method can throw the type of exception specified in Fault Contract.

Working sample

1. Create a simple WCF service with only one method. This method will have one output and two input parameters.

clip_image004

2. Implement the service contract

clip_image006

3. Press F5 to host the service in cassini server. Copy the URL from here.

4. Create a console client and add the service reference of the service we created.

5. Handle the exception at client side

clip_image008

6. Press F5 to get the exception information at client side. On running , you will get the exception message as below

clip_image010

So, we are getting the proper error message at the client side.

For your reference, source code is as below

Service side

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 FaultHandling

{

 

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        [FaultContract(typeof(DivideByZeroException))]

        double divide(int num1, int num2);

 

    }  

}

 

 

Service1.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace FaultHandling

{

 

    public class Service1 : IService1

    {

        public double divide(int num1, int num2)

        {

            if (num2 == 0)

            {

 

 

                DivideByZeroException myException = new DivideByZeroException();

                throw new FaultException<DivideByZeroException>(myException, “NUMBER 2 is 0”);

            }

            return num1 % num2;

        }

 

    }

}

 

 

 

Client side

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using ConsoleApplication3.ServiceReference1;

using System.ServiceModel;

 

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            Service1Client proxy = new Service1Client();

            try

            {

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

                Console.WriteLine(result);

            }

            catch (FaultException<DivideByZeroException> ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadKey();

        }

    }

}

 

 

Thanks for reading. I hope this article was useful for you. Happy Coding.

Advertisements

7 thoughts on “FAULT Contract in WCF with Learning Video

  1. Hi Dhananjay,

    Great article . I need ask to question. Can we use Fault contract in java application?

    Thanks
    Naushad

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