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();
}
}
}
Leave a Reply