Part 18 Throwing fault exceptions from a WCF service

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Link for all dot net and sql server video tutorial playlists

This is continuation to Part 17. Please watch Part 17, before proceeding.

In this video we will discuss throwing FaultException from a WCF service. The first question that normally comes to our mind at this point is that, why can't we simply throw .NET exceptions instead of Fault exceptions.

A WCF service should be throwing a FaultException or FaultException[T] instead of Dot Net exceptions. This is because of the following 2 reasons.
1. An unhandled .NET exception will cause the channel between the client and the server to fault. Once the channel is in a faulted state we cannot use the client proxy anymore. We will have to re-create the proxy. We discussed this in Part 17 of the WCF tutorial. On the other hand faultexceptions will not cause the communication channel to fault.
2. As .NET exceptions are platform specific, they can only be understood by a client that is also .NET. If you want the WCF service to be interoperable, then the service should be throwing FaultExceptions.

Please Note: FaultException[T] allows us to create strongly typed SOAP faults. We will discuss this in our next video.

using System.ServiceModel;
namespace CalculatorService
{
public class CalculatorService : ICalculatorService
{
public int Divide(int Numerator, int Denominator)
{
if (Denominator == 0)
throw new FaultException("Denomintor cannot be ZERO", new FaultCode("DivideByZeroFault"));

return Numerator / Denominator;
}
}
}

Client application:
using System;
using System.ServiceModel;
using System.Windows.Forms;

namespace WidowsClient
{
public partial class Form1 : Form
{
CalculatorService.CalculatorServiceClient client = null;

public Form1()
{
InitializeComponent();
client = new CalculatorService.CalculatorServiceClient();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
int numerator = Convert.ToInt32(textBox1.Text);
int denominator = Convert.ToInt32(textBox2.Text);

label1.Text = client.Divide(numerator, denominator).ToString();
}
catch (FaultException faultException)
{
label1.Text = faultException.Code.Name + " - " + faultException.Message;
}
}

private void button2_Click(object sender, EventArgs e)
{
client = new CalculatorService.CalculatorServiceClient();
MessageBox.Show("A new instance of the proxy class is created");
}
}
}
Рекомендации по теме
Комментарии
Автор

don't ever stop doing what you do. This is GREAT info.

playtime
Автор

When I explain things to myself in my head, I do so in your voice.

karrncares
Автор

Welcome Back, Sir. Very Happy to see u Back.

jeewanintube
Автор

Welcome Back Sir.  last 1 month = 1 year
waiting you BOSS

siciidh
Автор

Welcome back :).
Kindly upload Design Patterns, Sharepoint, WPF videos as well it will be very helpful

ananthswaroop
Автор

Hi Venkat,
I hope your doing well. I fallowed your tutorials each and every one If you feel free please upload WPF tutorials its very usefull for all your fallowers. Its my humble request. Thank You So much.

sunildesu