The async and await in ASP.NET MVC

The async and await combination allows you to have non-blocking methods.

In ASP.NET MVC you can have asynchronous actions. It might sound a good idea to make all your actions async, but it is not. Light operations like returning a view, handling a post, will likely get slower because the cost of creating a thread will be much higher.

But you can benefit from it by using it against I/O operations.

You should consider using async in these cases, so the worker thread is not jeopardized.

For example, if I am posting a model to the controller and then saving it to the database:

public ActionResult Index()
{
    Game game = new Game();
    return View(game);
}

[HttpPost]
public async Task<ActionResult> Index(Game game)
{
    bool saved = await SaveGameAsync(game);
    return View(game);
}

public async Task<bool> SaveGameAsync(Game game)
{
    using (var db = ApplicationDbContext.Create())
    {
        db.Games.Add(game);
        return await db.SaveChangesAsync() > 0;
    }
}

On the default template ASP.NET MVC creates for you there is the AccountController which is full of async actions that deals with database.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s