Friday, March 12, 2010

WCF – Exception Handling in Asynchronous Operations

Yesterday, I got a query regarding how to handle exceptions in case of asynchronous web service operations. Precise question was,

I am calling a WCF service using Asynchronous way,  the service may execute for some time, meanwhile if any exception occurred in the service side how can I come to know what happened in the service side?

I am not able to get the status in the Async Method.

There is a pretty simple solution available for this problem.

When the asynchronous method throws an exception, proxy catches it and when the client calls End Operation the proxy re-throws that exception object so that client can handle the exception. If a COMPLETION CALLBACK is provided, WCF calls that method immediately after the exception is received. Exception thrown is compliant with the fault contract and type of exception. Following code snippet shows how to do this;

Contract Code

public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(Exception))]
double Add(double n1, double n2);
}



Service Code




public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
throw new FaultException<Exception>(new Exception("Gaurav is sending exception"));
return n1 + n2;
}
}



Client Code




class Client
{
static void Main()
{
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.WriteLine();

GauravCalculator.CalculatorClient calcService = new GauravCalculator.CalculatorClient();
int sum = 0;
AsyncCallback completion = (result) =>
{
try
{
sum = (int)calcService.EndAdd(result);
Console.WriteLine("Sum is :" +sum.ToString());
calcService.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
};
calcService.BeginAdd(10, 10, completion, null);

Console.WriteLine();
}



Using this method you can receive any service side exceptions on client side.

~pEaCe~


Currently Listening to: Bekaraar from Pathshala (new Lucky Ali song)

6 comments:

Anonymous said...

[url=http://blogs.laverdadigital.com.mx/?u=videosealisia4]Altdo DVD Burner 4.4[/url] [url=http://www.pinskerdream.com/bloghoster/?u=videoseandrea8]Fantasy DVD Player Platinum 9.5.1[/url]
VirtualDubMod Magic Video Maker Pro 2.6
http://www.answerbag.com/profile/1237415 Magic Video Converter 8.0.3.18
[url=http://www.adulthostedblogs.com/?u=videoseanderson5]ProgDVB MPEG Editor[/url] [url=http://www.pinskerdream.com/bloghoster/?u=videosealberta5]KingConvert Apple TV Video Converter[/url]
Nidesoft DVD Audio Ripper 3.1.06 Click DVD Ripper 2.03
http://a12.alphagodaddy.com/?ref=http://30-days.org/user/EditProfile.aspx?ProfileSaved=true&url=http://30-days.org/members/Acala-Video-MP3-Ripper-2.9.1-93.aspx&leo=0 Agree Free All Video Converter Platinum
Allok 3GP PSP MP4 iPod Video Converter 4.2.0709
my icq:858499940385

Anonymous said...

Il semble que vous soyez un expert dans ce domaine, vos remarques sont tres interessantes, merci.

- Daniel

Anonymous said...

Hі! Ι've been following your website for a long time now and finally got the courage to go ahead and give you a shout out from Houston Texas! Just wanted to say keep up the great work!

My web-site :: irving taxicab

Anonymous said...

bookmarked!!, I really like your web site!

Review my blog ... online graduate certificate programs

Anonymous said...

Way cool! Some very valid points! I appreciate you penning this write-up plus the rest of the site is very good.


my blog post :: electronic cigarette comparison (http://phpfoxtemplate.socialloft.com)

Anonymous said...

I've been writing to get a small over annually on Examiner now and I'm finding convenient within my writing.

This amazing gadget was in almost every child's wishlist.


Here is my web site ... gta vice city tips