In the last week's article, I've provided an overview of the most important global improvements in .NET Framework 4.5 that affect the Common Language Run-time (CLR). In this article in this 3-part series analyzing .NET Framework 4.5, I examine the most useful new features and improvements included in ASP.NET MVC 4 and ASP.NET 4.5.
ASP.NET MVC 4
One of the most important features of ASP.NET MVC 4 is the introduction of the new ASP.NET Web API, which simplifies REST programming with a strongly typed HTTP object model. In addition, ASP.NET MVC 4 takes advantage of the new asynchronous programming model introduced with .NET Framework 4.5 to allow developers to write asynchronous action methods. It is important to understand the advantages and disadvantages of the new asynchronous methods to use them whenever they will provide a benefit.
(ASP.NET MVC 4 also includes many enhancements focused on mobile development, such as jQuery Mobile support and selecting views based on which mobile browser makes requests. If you work with previous ASP.NET MVC versions and you target multiple mobile devices, the new display modes are worth moving to ASP.NET MVC 4. In addition, the bundling and minification framework makes it simpler to reduce HTTP requests for each page without having to use third-party tools.)
If you haven't done so, I encourage you to read “Visual Studio 2012 for ASP.NET Developers” by Dino Esposito. Dino provides a detailed overview of the most important features for both ASP.NET MVC 4 and ASP.NET 4.5. In addition, he summarized the availability of the different features in a table that you can use as a useful reference. I will dive deeper in one specific feature and its usages, the asynchronous methods.
Asynchronous methods in ASP.NET MVC 4
.NET Framework 4.5 adds two new keywords to simplify asynchronous programming, on top of the task-based programming model introduced in .NET Framework 4 with the
System.Threading.Tasks namespace. In C# 5.0, the new keywords are the following:
async: This modifier marks a method or a lambda expression as asynchronous. When you are going to make asynchronous calls by using the await keyword, you need to add the async keyword to the method's signature.
await: This operator yields control until a task completes. By using this modifier, you don't have to create callback functions anymore. The compiler does the necessary work to transform your code into an asynchronous call and also handles the necessary conversion to return T, instead of
Task<T>. As a result, the code is really simpler to write and easier to understand. However, you can also return
The new model for asynchronous methods is known as "TAP" (short for Task-based Asynchronous Pattern). ASP.NET MVC 4 allows you to combine await, async and
Task<T> instances to write asynchronous action methods for controllers. However, it is very important to understand when asynchronous action methods are appropriate and when they aren't. If you worked with task-based programming approaches to achieve performance improvements when targeting multi-core CPUs, you might draw incorrect conclusions about asynchronous methods with ASP.NET MVC 4.
When you have CPU-bound operations in Windows applications you usually try to take advantage of parallel programming features included in .NET Framework in order to achieve performance improvements. Thus, you detect the hotspots and launch parallel tasks to take advantage of the available cores. However, when you have CPU-bound operations in ASP.NET MVC, they aren't good candidates for using asynchronous action methods. Asynchronous methods add an overhead and when the action methods execute CPU-bound operations, that overhead doesn't provide any benefit.
You might consider asynchronous methods for certain CPU-bound operations when you need to increase the responsiveness of some pages and the overhead makes sense because you have a benefit. Nevertheless, you should prioritize other methods for their conversion to asynchronous methods before adding an overhead to CPU-bound operations unless you want to provide a cancellation mechanism for long-running operations.