Fault Contract How to communicate error information from a service to a client
67K views
Nov 21, 2023
Fault Contract How to communicate error information from a service to a client
View Video Transcript
0:00
Good evening friends I am Dhanja Kumar and I am MEP on Connected System
0:06
In this particular video tutorial we are going to learn how we can work with fault contract in WCF
0:13
We will see that if you are not handling the exception at the service side
0:17
then how the whole proxy is getting failed while a single error is coming at a service site
0:25
and we'll look ahead and see how we can handle an exception
0:29
handle and exception and expose that exception using fault contract. So for the theoretical part of our technical definition of fault contract and description
0:39
of our contract you can refer my other articles. But here in this purpose of this video is to completely show you or rather give you a demo
0:48
that how we can achieve a fault contract. So let's go ahead and open your Visual Studio
0:54
I have Visual Studio 210. You can do the same thing in Visual Studio 2.2
0:58
or to wait also. Just go ahead and create a new WCF service application. What I'm going to do here
1:05
What I'm going to do is that first I'll show you my service without handling the exception
1:12
and then I'll show you that while we handle the exception how you are going to do that
1:16
Just go ahead from WCF tab, select WCF service application project template
1:22
Remember if you are using Visual Studio 2008 you will get WCF service application project template
1:28
under web tab not in WCF tab. Fine. Give some name here I'm giving his name demo fault service
1:41
What I'm going to do here is that I'll create a very simple service which will have only one operation contract and that will do operation of division
1:52
It's taking time here. here go ahead and delete whatever default code is given to you by WCF the
2:11
purpose of this default code is only to show you that how you can write a simple
2:16
service we really don't don't need those codes so what I done here is that I have
2:21
deleted all the default code which is getting created by WCF when we are
2:25
creating a WCF service I believe here all of you who is, anyone of you who is watching this video, they must be aware of what is operation contract and all
2:53
Just create an operation contract which will return a double. and which will take two number two parameters two number of the double type the
3:07
purpose of this of operation contract is very simple to or divide two number and
3:13
as I told you that our main core purpose of this particular video is to find
3:17
out how we can work with fault contract okay now go here and just implement
3:26
that so just copy paste that from here make it public return number one
3:46
divided by number two. This is a very simple implementation. If you see the code we are just dividing to number
3:59
and I am going to rely on the default binding which is given by WCF for in
4:04
for in for you and that is wave HTTP sorry WS HTTP binding
4:08
let's go ahead and run first compile your service run it
4:34
In the main time, again I want to say the same thing is that right now I have written
4:42
a service which is not handling the exception. So if some exception is coming at a service side, our client will get that exception in
4:53
term of that whole proxy will get corrupted because we are not handling that
4:57
We'll see that ahead. Up to this point our service is up and running
5:01
Fine. Just copy paste this particular URL that is our end point of our service
5:06
And what I'm going to do is that I'm going to open a new Visual Studio and go ahead and just create a console client
5:12
Fine enough, this is a very simple console client and add your service here as service reference
5:26
Copy place the end point URL, sorry, end point of your service. That's right
5:30
of your service does you are a if you see our service is successfully added
5:43
that the function and the name is space of your service my project name is
5:56
console application 3 so it's that now create a proxy all you know that
6:05
service 1 since our service name is service 1 so WCS is already created for your
6:09
service 1 client class you can create a proxy using that class what is a proxy
6:16
actually proxy is nothing but instance of your service at a client site service is
6:20
nothing but a class which is running on on an external machine so really to use that
6:25
class you need to instant say that class right so proxy is nothing but
6:29
instance of the service at a client's side now we have created that and if you
6:34
see prox we can call the div method like a simple other methods of a normal
6:41
class and here we need to pass a let us say to parameter 30 and 5 and passing and
6:55
and save the result in a variable code result. Print that. Now go ahead and run your client
7:14
What I did here is the first line is that I created a proxy of the service In second line normally you know very easily I calling or only one operation contract
7:26
given passing the two parameters, two double-intuitive parameter to that function. In third line, I'm just displaying the result which is coming from the service
7:38
Let's go ahead and run this particular client application. and here we are getting the expected result 0 right because when you divide 30 by 5
7:51
even there is 0 right now what I am going to show you is that just change your
7:57
number 2 from 5 to 0 now at a service side we are expecting or divide by 0 exception
8:04
because we cannot divide any number by 0 right so here some exception is coming right
8:10
and we have some exceptions we are expecting and that particular exception we have not handled at a service site so let's go ahead and again
8:17
run and see what's happening we are getting of you know some wire result
8:29
we are getting some unexpected you really don't know what is that it means exception is not handled right so what I'm gonna do is that let's handle
8:41
the exception and see how we can display a meaningful error message to our client that
8:47
what really happening. If we run again, we really are not getting any idea like what is happening, what is
8:52
NAN and whatever. In your case you might get a runtime, runtime exception
8:58
In my case I'm getting the NAN. If you are using Visual Studio 2008 you might get a runtime exception
9:04
So now go ahead to your service. this is my service create instance of divided by zero exception
9:27
and we check if number two is equal to equal to zero then only we need
9:39
to handle exception right Then what we'll do? To throw new fault exception, that is divided by zero exception
9:59
And that takes to parameter one is this exception. And other is you can say number two is zero at service
10:11
side. Fine. Now if you see this particular line that why I am using fault
10:27
exception slash T, fault exception divided zero exception because if you want to
10:32
propagate exception exception from service to a client we need to use the
10:38
generic fault exception class rather than rather than rather than the than the normal fault exception class which we normally use in our dot net programs
10:46
Because we need to, we really don't need, we really don't know that what type of exception
10:52
our service is going to throw at a runtime That is why we need a generic exception So the exception is really the exception which we are using here is that except is a generic exception and that is fault exception T
11:14
Now this T can take anything. This T can take any CLR type, this particular T can take any CLR type, this particular T can
11:22
take any data contract. Or in our case we are taking it as a data contract. it as a divided by zero exception. Fine
11:28
Now, we have implemented the exception or we can say in other words that we have handled
11:36
the exception at the service site. But really we need to say to our client that we have handled some exceptions
11:42
And that is done through something called fall contract. Type of divide by juries
12:25
So our services compile successfully is again run your service. Service is up and running
12:39
Now again go back to your client and update your service reference because we have done changes in our service
12:45
So we need to update our service reference such that our changes get reflected with a client
12:50
Now go ahead and run your service. run your client sorry and here you are getting number two is zero at a
13:07
service side right now what you need to do is that why we got this runtime
13:13
error because at a client side we have not handled the exception so what we
13:17
really need to do is that go to your client before calling this a normal simple dot net exception handling if exception is coming
13:33
so catch exception px console dot right line px dot masses now our output should be
13:53
Here we are. So number two is your service side. So here in this way using FFAR contract
14:00
and F F F F Contract T, the General F F F Contract Plus, we can pass our technology specific
14:07
exception to our generic error information to the client. So it was for me in this video for
14:16
you that how we can use the FFAR contract and pass the exception message to client
14:23
Thank you so much and in further videos I'll show you how we can pass data contact as an exception type to our client. Thank you so much
#Education
#Helpdesk & Customer Support Systems
#Programming
#Software