Part 20 Centralized exception handling in WCF by implementing IErrorHandler interface

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

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.

This is continuation to Part 19, please watch Part 19 before proceeding.

In this video, we will discuss - How to handle all WCF service exceptions in one central location. This is a very common interview
question.

redirect the user to a custom error page.

In WCF, to centralize exception handling and to return a general faultreason to the client, we implement IErrorHandler interface.

Let's now look at the 3 steps involved in centralizing exception handling in WCF. We will be continuing with the same example, that
we worked with in Part 19.

Step 1: Implement IErrorHandler interface.

IErrorHandler interface has 2 methods for which we need to provide implementation.
1. ProvideFault() - This method gets called automatically when there is an unhandled exception or a fault. In this method we have
the opportunity to write code to convert the unhandled exception into a generic fault that can be returned to the client. ProvideFault() gets called before HandleError() method.

2. HandleError() - This method gets called asynchronously after ProvideFault() method is called and the error message is returned to
the client. This means that this method allows us to write code to log the exception without blocking the client call.

Step 2: Create a custom Service Behaviour Attribute to let WCF know that we want to use the GlobalErrorHandler class whenever an
unhandled exception occurs. GlobalErrorHandlerBehaviourAttribute class
1. Inherits from Attribute abstract class.
2. Implements IServiceBehavior interface. This interface has 3 methods (Validate(), AddBindingParameters(),
ApplyDispatchBehavior()). The implementation for Validate() and AddBindingParameters() method can be left blank. In the
ApplyDispatchBehavior() method, we create an instance of the GlobalErrorHandler class and associate the instance with each
channelDispatcher.
3. Has a constructor that contains one Type parameter. We will use this constructor in Step 3.

attribute has one constructor that expects a single Type parameter. Pass GlobalErrorHandler class created in Step 1 as the
argument.
[GlobalErrorHandlerBehaviour(typeof(GlobalErrorHandler))]
public class CalculatorService : ICalculatorService
{
public int Divide(int Numerator, int Denominator)
{
}
}
Рекомендации по теме
Комментарии
Автор

Best learning channel ever, please never stop making videos, I'd do anything to support you

houssamfertaq
Автор

THANK YOU. After fruitless attempts to follow a couple of oblique tutorials, I found your clear example of centralized WCF exception handling and was able to implement (and more importantly, *understand*) a global handler in my WCF service. THANK YOU.

ScottGammans
Автор

Great tutorial - thank you. I scrolled through so many other tutorials without success, but this one told me exactly what I needed to know, and explained how it works very clearly.

LaurenceFrost
Автор

Great Video, one of the best .NET channels in youtube.
congratulations

sikamikan
Автор

What a fantastic way of teaching.Really helpful to us...

Himanshupandey
Автор

sir you have explain this complex topic so easily. Thank you so much sir. This will really help us to implement things practically and explaining the same to colleagues/interviewer.

ronyissporty
Автор

Thank you very much for the very elegant and concise video. This is exactly what I was looking for.

cnoshiri
Автор

I got clear ideas of some main sql server topics, thanks for your valuable viedos.

ismailcbewin
Автор

Awsm tutorial ..good job +kudvenkat !

ArjunSingh-zjjt
Автор

Muchas gracias por compartir tus conocimientos es de mucha ayuda para muchos que estamos aprendiendo nuevas tecnologías, actualmente tengo un requerimiento que no he podido darle solución te platico el requerimiento es que pueda mandar un error personalizado cuando manden un xml de envió incorrecto y que se pueda mostrar personalizado pero no lo he logrado, lo estoy probando con SOAPUI pero no he podido hacerlo he intentado con FaultException y FaultContracts pero sin éxito. no se si tengas alguna idea de como poder lograrlo

eduardoreyesmoreno
Автор

Your tutorials are awesome Venkat. Can you upload videos on design patterns?

mailmeveek
Автор

When my Validate method gets called, is always zero, so the foreach loop is skipped and the handler is not added to any channelDispatchers.
What am I doing wrong?

gibbytravis
Автор

Hi.
When I tried passing the action argument as follows:
fault = Message.CreateMessage(version, faultMessage, "It is a generic error");
Am getting communication exception at my client side.
The rest of the application is as same as yours. only difference is this string action aargument. With it as 'null', am not getting any exceptions.
I could not understand what wrong with that and why a "CommunicationException" occurs shutting down the channel.

rajagopalramamoorthy
Автор

Sir Thanks For Your Videos in wcf, Please Upload Some Of the videos Regarding Wcf Security And Transaction and How to implement those security concepts in wcf with some Detail Examples

biswaprakashswain
Автор

Your Tutorials are More useful for us we are all thank u for your
Can u Upload Videos For

shreesabarathinam
Автор

Great videos! Sir do you have any videos on Xamarin Mobile development? Or recommend a source to me? Thanks

EbenezerFoh
Автор

how to remove is error (Client is unable to finish the security

lovenice
Автор

How to show this exception in client application

nrkhiable