SIGN UP MEMBER LOGIN:    
ARTICLE

Fault Contract in WCF With Learning Video

Posted by Dhananjay Kumar Articles | WCF with C# May 24, 2010
This article will give basic introduction on how to handle exception at service side, how to use fault contract at service side and how to handle service exception at client side.
Reader Level:

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?
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.
1.gif

SOAP Fault 

2.gif
 
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. 

 3.gif

2. Implement the service contract 

4.gif 

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 

5.gif 

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

6.gif
 
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. 

Login to add your contents and source code to this article
share this article :
post comment
 

How to throw fault excpetion when we do not know what error the WCF service may retutn I mean when we know that it is a DivideByZeroException we can use it directly if we do not know the exception type how to proceed?

Posted by Mohan krishna Jul 21, 2011

Good One Dhanjay...Keep Going

Posted by Vijay Dahite Dec 09, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor