0
webcontroller {
         async Task<ActionResult<string>> doSomething() {
            var stringResult = await doSomethingAsync();
            return stringResult;
         }
}

what will be control flow here? will the controller return dummy response (ActionResult) to client after reaching doSomething() method call or the control remain in the web controller and return the stringResult to client? consider doSomething() is doing some network intensive tasks which might take more time to complete. Can anyone please explain the same to me if possible? Thanks in Advance!

Jawahar_GCT
  • 73
  • 1
  • 4

2 Answers2

1

will the controller return dummy response (ActionResult) to client after reaching doSomething() method call or the control remain in the web controller and return the stringResult to client

It will not return anything to the client until doSomething method finished.

consider doSomething() is doing some network intensive tasks which might take more time to complete

In this case you will have timeout on the client.

You have to start background job. Return to the client that task has been started. Then tell somehow to the client that task is finished.

Another source of information: Long running task in WebAPI

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
  • Thanks for the explanation. Consider I have scheduled the job in the background thread using Task. Run and I have put the task and operation id of the task in the server side to check the status from the client side (consider I have exposed another url and using the operation id client can check the status). Then there is no need of returning the Task> from the controller, right? I can simply return the ActionResult. Am I understood properly? – Jawahar_GCT Jul 09 '21 at 12:24
  • Yes, it is not rule to return Task in controller. But if controller uses other async API - then controller method MUST be async. – Svyatoslav Danyliv Jul 09 '21 at 12:27
1

I recommend reading an article I wrote about how async works on ASP.NET.

will the controller return dummy response (ActionResult) to client after reaching doSomething() method call or the control remain in the web controller and return the stringResult to client?

When doSomethingAsync returns an incomplete task, then the await in doSomething will also return an incomplete task. Then the ASP.NET runtime (asynchronously) waits for that task to complete before sending the response.

await in ASP.NET yields to the thread pool; it does not yield to the client.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Sorry stephen. I am not able to understand your statement. **Then the ASP.NET runtime (asynchronously) waits for that task to complete before sending the response** doSomething() returns an incomplete task, which is currently executed by background thread. Say the background task will be completed in 50 secs. Now variable stringResult contains the incomplete task. do you mean the controller will wait untill the returned incomplete to finish (wait for 50 second after receiving incomplete task) or it will return the incomplete task response (like work started)? – Jawahar_GCT Jul 10 '21 at 08:17
  • @Jawahar_GCT: The controller returns an incomplete task to the ASP.NET runtime. The ASP.NET runtime (the thing that *calls* the controller) will wait for the task to complete before sending any response to the client. On a side note, [there is no background thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html). – Stephen Cleary Jul 10 '21 at 14:43