0

In some article about asynchronous web api 2 actions, I have found following code that shows how to implement async operations in Web Api 2:

 public class UserController : ApiController
        {
            ApiSecurityEntities _db = new ApiSecurityEntities();

            public async Task<IHttpActionResult> Delete(Int32 Id)
            {
                 var record = await _db.UserMaster.Where(f => f.id == Id).FirstOrDefaultAsync();
                 if (record != null)
                 {
                     _db.UserMaster.Remove(record);
                     await _db.SaveChangesAsync();
                     return Ok();
                 }
                 return NotFound();
            }
        }

I suppose that this code is not async, but it's only wrapped with async/await words. Can you dispel my doubts?

#EDIT

Reasons why I'm supposing that isn't truly async code:

  • After first await there is only if statement that must wait for previous operation and no other work will be done after this block, so it will be run synchronously
  • After second await there is return statement, so it must wait until _db.SaveChangesAsync(); will be done
mkul
  • 773
  • 1
  • 10
  • 28

1 Answers1

2

This code is absolutely real async in terms of I/O.

In computer science, asynchronous I/O, or "Non-sequential I/O" is a form of input/output processing that permits other processing to continue before the transmission has finished.

From wikipedia.

In your case your method does not block any thread while you're waiting for I/O to completes (DB operations), using await you tell the current thread that it can return to the ThreadPool and do any other work before the I/O operation completes.

Any thread (some kind of processing unit) will not be blocked while the DB doing his job which follows the asynchrouns definition.

#EDIT

As you said your code execution order does not change and it needs the results from the DB to execute the next statements, but(!) although it seems that this code is blocking (aka sync) it will not block any thread while the I/O operation executing.
The await keyword allows you to "wait" in an async way for the results and when the I/O is done it will continue executing the rest of the method.

YuvShap
  • 3,825
  • 2
  • 10
  • 24
  • So it's better to put all database operations in `async/await` even if code looks like it will run operations synchronously? – mkul May 11 '17 at 11:32
  • Exactly, by doing this you can use your resources (threads) in more effective way, instead of keeping your threads waiting for no reason to the external operation they can do other jobs, such as answering other requests, this principle can really help server scalability. – YuvShap May 11 '17 at 11:37
  • @mkul You seem to repeatedly miss the point of async/await. It is not about multithreading or executing code in parallel. Also please note that in the specific case of ASP.NET the benefit of using async is [scalability rather than performance](http://stackoverflow.com/a/36684289/11683). – GSerg May 11 '17 at 11:43