Channels ▼
RSS

Tools

Using Asynchronous Methods in ASP.NET 4.5 and in MVC 4


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 Task<T> whenever needed.

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video