Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF design template needed for nested asynchronous methods (especially Silverlight)
    primarykey
    data
    text
    <p>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?</p> <p>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? </p> <p>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.</p> <p>// PSEUDOCODE:</p> <p>// assume FirstWebService returns a collection of objects, say MyObjects, in a List // assume SecondWebservice takes this collection and does something with it.</p> <p>ServiceReference1.Service1Client aClient; private List myObjectList = new List();</p> <p>public Page1() { InitializeComponent();</p> <p>aClient = new myWebService.ServiceReference1.Service1Client(); </p> <p>aClient.FirstWebService += new EventHandler(aClient_FirstWebServiceCompleted);</p> <p>aClient.SecondWebServiceCompleted += new EventHandler(aClient_SecondWebServiceCompleted); </p> <p>}</p> <p>//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.</p> <p>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)</p> <p>}</p> <p>void aClient_FirstWebServiceCompleted(object sender, myWebService.ServiceReference1.FirstWebServiceCompletedEventArgs e) {</p> <pre><code> 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. </code></pre> <p>//Here is the ‘recursive’ (loop) part: </p> <p>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. } }</p> <pre><code> else { //handle errors here } </code></pre> <p>}</p> <p>void aClient_SecondWebServiceCompleted(object sender, myWebService.ServiceReference1.SecondWebServiceCompletedEventArgs e) {</p> <pre><code> if (e.Error == null) { // do stuff here = e.Result; //etc </code></pre> <p>} else { //error trap here, since e.Error != null } }</p> <p>/* // 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?</p> <p>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.</p> <p>*/</p> <p>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)</p> <p><a href="http://old.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx" rel="nofollow">http://old.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx</a></p> <p>also see www.bluebytesoftware.com /blog/2008/02/18/IDisposableFinalizationAndConcurrency.aspx</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload