Video of the article is
HERE
Objective
This article will give basic introduction on
- How to handle Exception at service side?
- How to use Fault contract at Service side?
- How to handle Service Exception at client side?
Few points about Exception at Service
- Exception is technology specific.
- Exception should not share beyond service boundary.
- Since Exception is technology specific they cannot be propagated to client.
- Exception are of types
- CLR Exception
- Windows32 Exception
- Runtime Exception at service
- C++ Exception
- Exception are very much native to the technology in which service is made.
- Exception must be converted from technology specific information to natural information that can be communicated to client.
SOAP Fault
FaultException<T>
- Service should throw FaultException<T> instead of usual CLR exception.
- FaultException<T> is specialization of Fault Exception.
- Any client that programs against FaultException can handle exception thrown by FaultException<T>.
- The type parameter T conveys the error detail.
- T can be of any type like Exception, CLR Type or any type that can be serialized.
- T can be of type Data contract.
- T is a generic parameter conveys the error details.
About Fault Contract
- Any Exception service want to share beyond service boundary comes must be goes at the part of service contract.
- Fault contract says what type of exception service can throw to client.
- The type in Fault Contract must be the same as the T of FaultException<T>
- Fault contract is the contact between service and client about what exception can be thrown from service to client.
- 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.
2. Implement the service contract
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
6. Press F5 to get the exception information at client side. On running , you will get the exception message as below
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.