2

We plan to use AsyncController, since most of our requests will be long-running, I/O bound requests. The plan is to offload this processing to CLR threads, in order to keep the highest number of IIS threads free to service new incoming requests.

So far, every example we have seen of using AsyncController executes the long-running, I/O bound processes asynchronously from inside the AsyncController's asynchronous action method. We can see the value of doing so, if you had 2 or more operations that you could run in parallel in your Controller's Async action method.

Example: Using an Asynchronous Controller in ASP.NET MVC

In there, they execute the news service asynchronously. Is this really required? It seems redundant to execute the news service asynchronously if it is the only thing that you need to execute in that action method. In our case, we have a long I/O process to run. Can't we just run it synchronously from within the async version of our action method? Won't the work have already been passed off to a CLR thread once we are inside the asynchronous action method in the AsyncController?

Again, we can see how we would want to execute further actions asynchronously if we had a lot more work to which could be run in parallel. However, we only have one long blocking operation, and would prefer to keep the code simple.

We also saw another tactic, using Task.Factory.StartNew(), as shown here: Using the AsyncController in ASP.NET MVC 2

We would very much prefer not to do it this way, either, as it seems needlessly redundant.

Pittsburgh DBA
  • 6,672
  • 2
  • 39
  • 68

1 Answers1

2

Yes, I believe you are correct. The documentation states the following steps for initializing an async action.

  1. The Web server gets a thread from the thread pool (the worker thread) and schedules it to handle an incoming request. This worker thread initiates an asynchronous operation.

  2. The worker thread is returned to the thread pool to service another Web request.

The worker thread initiates an asynchronous operation (ie, you're not required to do it in the code). The use of "extra" asynchronous code in the examples you cite is likely meant to illustrate the use of AsyncManager.OutstandingOperations.Increment and Decrement (which inform the worker process that an operation is still running, even though the Async action may have returned).

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Based on some testing today, it seems that, while our understanding may be correct, this may be unwise, as it defeats the purpose. Here is question I have about getting beyond an apparent limit: [Azure Web Role Stress Test - 1000ms blocking operation in AsyncController](http://stackoverflow.com/questions/12549805/azure-web-role-stress-test-1000ms-blocking-operation-in-asynccontroller) – Pittsburgh DBA Sep 23 '12 at 05:01