Alan Alan - 3 months ago 10
ASP.NET (C#) Question

What's the benefit with using Async in an ASP.Net WebAPI method with EF?

I recently asked a question and was given two answers:

The sync version:

var phrasesCount = db.Phrases.Count();
The async version (assuming this is EF):

var phrasesCount = await db.Phrases.CountAsync();

Can someone explain to me what the difference is and why I might want to use Async?


The worker process where your app is running has a limited number of threads available to handle http requests. The goal is to keep those threads free so that they are available to handle incoming requests. The async version does not block the calling thread. This allows that thread to get back to handling those incoming http requests. In the meantime, the async method has been fired off and when it completes, execution will pick up where it left off at the await operation. If you are running your app and testing the difference between the two using your example, you will not see much of a difference between performance. The real value comes when your app has more requests incoming than there are threads available to process them.