Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async and Await in ApiController Post

I'm still not quite clear about async and await in .net 4.5. So far, I think I understand that await:

  1. puts the function (to its right) on a separate thread.
  2. puts the execution back to the current function's caller
  3. but holds the rest of the current function's code "hostage" until the awaiting (async) function is finished.

Please correct me if I'm misunderstanding something. So, if the above is true, I'm stuck with an ApiController's Post function that I want async:

[HttpPost]
public async Task<HttpResponseMessage> Post([FromBody]MyObject obj)
{        
     myDataContext.MyObjects.InsertOnSubmit(obj);
     myDataContext.SubmitChanges();

     await SomeReallyLongRunningTaskAsync();        

     // obj would now have the new Id, which I'm really after.
     return Request.CreateResponse(HttpStatusCode.Created, obj);

}

So if I'm understanding this correctly, Post will finish execution and return control to whoever called myApiController.Post(obj). But I don't have the HttpResponseMessage object yet since await held return Request.CreateResponse(HttpStatusCode.Created, obj); "hostage".

In this above simple example, would the call immediately return to the client (that is, client JS website or mobile app)? If so, would it be a 201, 400, 500 (better not), others?

like image 684
Mickael Caruso Avatar asked Oct 10 '13 21:10

Mickael Caruso


People also ask

Why we use async and await in Web API?

It is a programming technique that allows us to execute our flows without blocking our application or causing the thread pool starvation. The often misconception is that by using the async and await keywords we gain better performance in terms of the speed of our application.

Can we use async await in Web API?

An asynchronous method allows you to start a long-running operation, returns your thread to the pool, and wakes up on a different thread or the same depending on the availability of threads in the pool at that time. Now create an application. Create a Web API application as in the following: Start Visual Studio 2012.

Is ASP NET core asynchronous?

All I/O in ASP.NET Core is asynchronous. Servers implement the Stream interface, which has both synchronous and asynchronous overloads. The asynchronous ones should be preferred to avoid blocking thread pool threads.

How use async await in asp net?

You can use the await keyword only in methods annotated with the async keyword. The await keyword does not block the thread until the task is complete. It signs up the rest of the method as a callback on the task, and immediately returns.


1 Answers

In addition to Stephen's answer I need to point out a few things.

First, async in the controller does not make the user experience async. User will have to wait as long as SomeReallyLongRunningTaskAsync() takes. [So why do we do async? See the next point]

Also, if the SomeReallyLongRunningTaskAsync() is CPU-bound, then you should not call it in async mode. The main reason to use Async in a server scenario is to release the CLR thread back to the pool so that the IO Completion Port (IOCP) can handle the rest - until IO work done which then goes back to the thread pool. This will prevent the problem of Thread Starvation which is common in ASP.NET Scenarios.

IOCPs are used ONLY in IO-bound situations, examples are:

  • reading from/writing to a file
  • accessing database or
  • accessing an external Web Service or WCF service

There are tons of resources available online and explain various aspects. If I may put a plug, Chapter 2 of this book is an excellent resource which gives a cohesive understanding of Async in Web API.

like image 136
Aliostad Avatar answered Sep 30 '22 13:09

Aliostad