Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async controller in ASP.NET MVC

I checked the System.Web.Mvc.AsyncController in MVC 4.0, it has the comment "Provided for backward compatibility with ASP.NET MVC 3." does this mean there is a new implementation of async controller in MVC 4? what's the correct way in MVC 4.0 enable async a controller in order to put the I/O intense operations in other thread pool other than IIS request thread pool?

like image 407
Shuping Avatar asked Nov 09 '12 05:11

Shuping


1 Answers

Starting from ASP.NET MVC 4, you can now use the System.Web.Mvc.Controller class as the base class and leverage the TAP (Task-based Asynchronous Pattern):

public async Task<ViewResult> Index() { 

     return View(await GetThingsAsync());
}

Note that you don't have to use async and await keywords which come with C# 5.0 but they make asynchronous programming much, much easier and more maintainable.

Have a look at the following articles:

  • Using Asynchronous Methods in ASP.NET MVC 4
  • My Take on Task-based Asynchronous Programming in C# 5.0 and ASP.NET MVC Web Applications
  • Asynchronous Database Calls With Task-based Asynchronous Programming Model (TAP) in ASP.NET MVC 4
like image 80
tugberk Avatar answered Sep 28 '22 06:09

tugberk