Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC2 AsyncController: Does performing multiple async operations in series cause a possible race condition?
    primarykey
    data
    text
    <h1>The preamble</h1> <p>We're implementing a MVC2 site that needs to consume an external API via https (We cannot use WCF or even old-style SOAP WebServices, I'm afraid). We're using <code>AsyncController</code> wherever we need to communicate with the API, and everything is running fine so far.</p> <p>Some scenarios have come up where we need to make multiple API calls in series, using results from one step to perform the next.</p> <p>The general pattern (simplified for demonstration purposes) so far is as follows:</p> <pre><code>public class WhateverController : AsyncController { public void DoStuffAsync(DoStuffModel data) { AsyncManager.OutstandingOperations.Increment(); var apiUri = API.getCorrectServiceUri(); var req = new WebClient(); req.DownloadStringCompleted += (sender, e) =&gt; { AsyncManager.Parameters["result"] = e.Result; AsyncManager.OutstandingOperations.Decrement(); }; req.DownloadStringAsync(apiUri); } public ActionResult DoStuffCompleted(string result) { return View(result); } } </code></pre> <p>We have several Actions that need to perform API calls in parallel working just fine already; we just perform multiple requests, and ensure that we increment <code>AsyncManager.OutstandingOperations</code> correctly.</p> <h1>The scenario</h1> <p>To perform multiple API service requests in series, we presently are calling the next step within the event handler for the first request's <code>DownloadStringCompleted</code>. eg,</p> <pre><code>req.DownloadStringCompleted += (sender, e) =&gt; { AsyncManager.Parameters["step1"] = e.Result; OtherActionAsync(e.Result); AsyncManager.OutstandingOperations.Decrement(); } </code></pre> <p>where <code>OtherActionAsync</code> is another action defined in this same controller following the same pattern as defined above.</p> <h1>The question</h1> <p>Can calling other async actions from within the event handler cause a possible race when accessing values within <code>AsyncManager</code>?</p> <p>I tried looking around MSDN but all of the commentary about <code>AsyncManager.Sync()</code> was regarding the BeginMethod/EndMethod pattern with <code>IAsyncCallback</code>. In that scenario, the documentation warns about potential race conditions.</p> <p>We don't need to actually call another action within the controller, if that is off-putting to you. The code to build another <code>WebClient</code> and call <code>.DownloadStringAsync()</code> on that could just as easily be placed within the event handler of the first request. I have just shown it like that here to make it slightly easier to read.</p> <p>Hopefully that makes sense! If not, please leave a comment and I'll attempt to clarify anything you like.</p> <p>Thanks!</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.
 

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