Take the 2-minute tour ×

Say you are calling two methods in WCF from the client side, with one web method recursively (or rather iteratively) calling the other web method in a sort of nested manner. A simple example below in pseudo-code, that works but I don't think I have the proper exception handling in place. What is the best design template for the client and for the server to handle situations where the server fails?

I read the references 1-3 below and they seem to imply you must be disposing of things on either the client or server side--is this correct?

I am using WCF in Silverlight 3.5, using Visual Studio 2008 with C# as the code-behind language, but I don't think that matters that much, as this is a universal question.


// assume FirstWebService returns a collection of objects, say MyObjects, in a List // assume SecondWebservice takes this collection and does something with it.

ServiceReference1.Service1Client aClient; private List myObjectList = new List();

public Page1() { InitializeComponent();

aClient = new myWebService.ServiceReference1.Service1Client();

aClient.FirstWebService += new EventHandler(aClient_FirstWebServiceCompleted);

aClient.SecondWebServiceCompleted += new EventHandler(aClient_SecondWebServiceCompleted);


//now when you click the button, you will call the first web service which will then call the second web service (many times), until the foreach loop is exhausted.

private void Button1_Click(object sender, RoutedEventArgs e) { aClient.FirstWebServiceAsync(); //calls first web service (which in turn will call a second web service "recursively" (or actually repeatedly), see below)


void aClient_FirstWebServiceCompleted(object sender, myWebService.ServiceReference1.FirstWebServiceCompletedEventArgs e) {

         if (e.Error == null)  

           myObjectList = e.Result; //assume both server side myObject and client side myObject are the same thing, so no type checking compiler error here with the Lists.

//Here is the ‘recursive’ (loop) part:

foreach (myObject O in myObjectList) { aClient.SecondWebServiceAsync(O); //note the recursive, nested asynchronous call from the first web service to the second web service here...done many times until the foreach loop is exhausted. } }

            //handle errors here


void aClient_SecondWebServiceCompleted(object sender, myWebService.ServiceReference1.SecondWebServiceCompletedEventArgs e) {

         if (e.Error == null)  

           // do stuff here = e.Result; //etc

} else { //error trap here, since e.Error != null } }

/* // I have tried the above and it works, and has not failed yet. You have each web service "doing their thing" asynchronously. So far it's worked Ok, but the web methods are not complicated and perhaps that's why it's worked. But I don’t think I have the proper scaffolding in place in case the web service fails—say it was taken off line by a power outage? I have a bad feeling I’ll get errors. How to make it more robust?

Also, the First and Second web services are LINQ to SQL methods, wrapped in ‘using’ blocks with TransactionScope to allow rollback of the methods in case they fail on the server side.


References 1-3: (as a Noob I am not allowed to post more than one link by Stack Overflow, so I'll post just the second link)


also see www.bluebytesoftware.com /blog/2008/02/18/IDisposableFinalizationAndConcurrency.aspx

share|improve this question

1 Answer 1

up vote 0 down vote accepted

As a practical matter I found letting the garbage collector do its magic works fine--and handle any exceptions the best you can, typically client side.

share|improve this answer

Your Answer


