Chapter 12. Async in ASP.NET Applications

The majority of .NET developers write web applications. Async brings some new performance possibilities to web server code, so we’ll look at how to use async within web applications.

Advantages of Asynchronous Web Server Code

On a web server, responsiveness during a request isn’t an issue in the same way that it is in UI code. Instead, the performance of a web server is measured by its throughput and latency, and how consistent those are.

Asynchronous code, on a heavily loaded web server, requires fewer threads than synchronous code to do the same amount of work. Each thread has a large memory overhead, and the bottleneck for web servers is often memory capacity. When memory is scarce, the garbage collector has to run more often, and usually does more work in total. If the memory used doesn’t fit into the physical memory of the server, it has to be paged to a disk, which can be very slow if the memory is used again soon.

It’s been possible to write asynchronous web server code in ASP.NET since version 2.0, but doing so was difficult without language support. For most people, the option to add more servers and load balance between them was more cost effective. With C# 5 and .NET 4.5, it becomes easy enough that it’s worth everyone taking advantage of the efficiency.

Using Async in ASP.NET MVC 4

ASP.NET MVC 4 and later, when run on .NET 4.5 or later, has full support for the Task-based Asynchronous Pattern, so we can use async methods. The important ...

Get Async in C# 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.