What is fault Contract?
A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client side. The purpose of a Fault Contract is to handle an error by the service class and display in the client side. Whenever the client makes a call to a service class and an unexpected exception comes such as, SQL Server is not responding, or Divide by zero; in this case the service class throws an exception to the client using the Fault Contract.
By default when we throw an exception from a service, it will not reach the client. WCF provides the option to handle and convey the error message to the client from a service using a SOAP Fault Contract.
To support SOAP Faults, WCF provides the FaultException class. This class has two forms:
The Fault Contract sample demonstrates how to communicate error information from a service to a client. The sample is based on the, with some additional code added to the servie to convert an internal exception to a fault.
now add a new Console Project in your Solution. add the ServiceReference in your Console Project .
Now debug the Console Application. you will get the Fault Exception generated by Service end.
the Complete Demo Project is attached: Click me!
A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client side. The purpose of a Fault Contract is to handle an error by the service class and display in the client side. Whenever the client makes a call to a service class and an unexpected exception comes such as, SQL Server is not responding, or Divide by zero; in this case the service class throws an exception to the client using the Fault Contract.
By default when we throw an exception from a service, it will not reach the client. WCF provides the option to handle and convey the error message to the client from a service using a SOAP Fault Contract.
To support SOAP Faults, WCF provides the FaultException class. This class has two forms:
- FaultException: to send an untyped fault back to the caller
- FaultException<T>: to send a typed fault data to the client. It is basically a Generic Type.
Syntax
[OperationContract]
[FaultContract(typeof(MyException))]
string getDetails(int value);
The Fault Contract sample demonstrates how to communicate error information from a service to a client. The sample is based on the, with some additional code added to the servie to convert an internal exception to a fault.
Now we are going to implement the Fault Contract .
Create a new Project . add WCF Application Project. the Code are :
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace FaultContractApp
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract (typeof(CustomException))]
int add(int num1, int num2);
}
[DataContract()]
public class CustomException
{
[DataMember()]
public string Title;
[DataMember()]
public string ExceptionMessage;
[DataMember()]
public string InnerException;
[DataMember()]
public string StackTrace;
}
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace FaultContractApp
{
public class Service1 : IService1
{
public int add(int num1, int num2)
{
CustomException _fault = new CustomException();
_fault.ExceptionMessage = "Exception occured in the service!";
_fault.InnerException = "Inner EXception";
_fault.Title = "Exception Title";
_fault.StackTrace = "Stack Trace message from service.";
throw new FaultException<CustomException>(_fault,"Reason: Testing the Fault contract");
}
}
}
now write the Code to use Service . the Code are :
using System.ServiceModel;
using myConsole.ServiceReference1;
namespace myConsole
{
class Program
{
static void Main(string[] args)
{
Service1Client _service = new Service1Client();
try
{
int result = _service.add(11, 55);
Console.WriteLine(result);
Console.ReadLine();
}
catch (FaultException<CustomException> ex)
{
Console.WriteLine(string.Format("Title -> {0} ", ex.Detail.Title));
Console.WriteLine(string.Format("ExceptionMessage -> {0} ", ex.Detail.ExceptionMessage));
Console.WriteLine(string.Format("Inner Exception -> {0} ", ex.Detail.InnerException));
Console.WriteLine(string.Format("StackTrace -> {0} ", ex.Detail.StackTrace));
Console.ReadLine();
}
}
}
}
0 comments:
Post a Comment