I recently inherited an ASP.NET MVC project. In that project, the developer is using async
everywhere. I'm trying to assess if its a good idea or not. Specifically, I'm reviewing the controller code at the moment.
In the controllers, the developer wrote the stuff like the following:
public async Task<ActionResult> Index()
{
return View();
}
Is there any advantage to this instead of the traditional version:
public ActionResult Index()
{
return View();
}
I could understand using async
if await
was used in the controller code. A lot of times, its not being used though. Is there any justification for this approach?
No. That's not a good idea to use Async everywhere.
Regarding the missing await the compiler issues a warning when an Async
method does not contain an Await
operator. An Async method without an await will run synchronously, which makes the semantics incorrect.
For your specific example, the operation is simple and short running, and that's why using Async/Await does not bring any benefits, and it shouldn't be used, so you are perfectly fine using:
public ActionResult Index()
{
return View();
}
Async/Await should be used for controller methods that perform some kind of IO (e.g. Network requests, Database access etc.). If a controller method is doing CPU bound work (e.g. math. calculations), then using Async/Await can actually make performance worse, unless your measurements prove me wrong.
The old rule applies: Measure twice, cut once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With