I am developing an Asp Net MVC client that will use Asp Net Web Api. And I have to decide on how to better design my application. After searching the web for a while I found people suggesting (for example, here) to make MVC action methods asynchronous. I also found out that the main benefit of having asynchronous action methods is scalability of the server, i.e., the server will be able to serve more requests. There is one thing though, the web api I'm consuming has synchronous action methods, and will run on the same server. So my guess is there are no benefits for me to implement asynchronous action methods for my MVC, because even if my MVC action methods are asynchronous and the server will be able to scale from the "MVC point of view" in the end these methods will still consume synchronous Web Api methods, and because of this, the server will "inevitably" run out of its thread pool. Maybe I'm missing something or there are some other benefits of asynchronous action methods?
Here is a very simple sample code I wrote to make you better understand my issue:
This is the web api controller:
public class UsersController : ApiController
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
// As you can see this method is not asynchronous
public User Get(int id)
{
return _userService.GetUserById(id);
}
// some other code
}
This is the Mvc controller, I have two choices how to design my action methods:
public class UsersController : Controller
{
// A) Make simple synchronous action methods
public ActionResult UserPageSync()
{
IUserWebServiceSync userWebServiceSync = new UserWebServiceSync();
User user = userWebServiceSync.GetUserById(1);
return View();
}
// B) Make asynchronous action methods
public async Task<ActionResult> UserPageAsync()
{
IUserWebServiceAsync userWebServiceAsync = new UserWebServiceAsync();
User user = await userWebServiceAsync.GetUserByIdAsync(1);
return View();
}
}