Jawahar_GCT
Jawahar_GCT

Reputation: 113

Async Await control flow in Web API

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!

Upvotes: 0

Views: 162

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456477

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.

Upvotes: 1

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27282

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

Upvotes: 1

Related Questions