Working with Asynchronous Operations in ASP.NET MVC
Introduction
Whenever you call action methods in an ASP.NET MVC application, by default they are executed in synchronous fashion. In order to improve the overall performance and responsiveness of your application you may wish to execute the code asynchronously. You may execute code asynchronously from two distinct places - server-side and client-side. This article discusses both of these scenarios and shows how to create asynchronous controllers and use SessionState attribute.
What are Asynchronous Operations?
Before you write any code that runs asynchronously, it would be nice to quickly brief what asynchronous operations are and the benefit of such operations. Suppose that you have a piece of code as shown below :
public void ParentMethod() { ChildMethod1(); ChildMethod2(); ChildMethod3(); }
The ParentMethod() is a method that you are supposed to call from the rest of your application. The ParentMethod() in turn calls three child methods viz. ChildMethod1(), ChildMethod2() and ChildMethod3(). Also assume that all the child methods are remote methods. Under synchronous execution (default as far as server-side code in ASP.NET is concerned) the three child methods will execute serially i.e. one after the other. Thus ChildMethod1() will execute first, will return the control back to the parent method and only then will ChildMethod2()be executed. If the child methods are taking say 5, 10 and 15 seconds to complete, the effective time taken for executing all of them will be 5 + 10 + 15 i.e. 30 seconds.
Under the asynchronous scheme of code execution, the child methods are called in parallel i.e. ChildMethod2() will start its execution without waiting for ChildMethod1() to complete. So, if the three methods are taking 5, 10 and 15 seconds to complete, the effective time taken will not be 30 seconds as in the former case but 15 seconds (the time taken for the longest running method).
Of course, there are other factors that affect the execution of asynchronous code but the above example highlights the main difference between synchronous and asynchronous operations.
As far as ASP.NET MVC is concerned there are two distinct places where asynchronous operations can be invoked:
- Server-side
- Client-side
In the former case you will use asynchronous controllers and in the latter case you will go for session-less controllers on the server along with AJAX code at the client-side.
Creating Asynchronous Controllers
Whenever you add a controller to your ASP.NET MVC application, you will see a class declaration like this:
public class HomeController : Controller { ... }
By default, the newly added controller class is inherited from Controller base class. If you wish to code asynchronous action methods you should inherit the controller class from AsyncController class instead.
public class HomeController : AsyncController { ... }
When you inherit your controller from AsyncController base class, an instance of AsyncManager class is made available to your code. The AsyncManager class allows you to flag asynchronous operations and also helps you to store pieces of data that are passed to the method handling asynchronous operation completion. Use of AsyncManager will be clear when you develop an example in later sections.
Creating Asynchronous Action Methods
The asynchronous controller declared, as shown in the preceding section, can contain asynchronous action methods. ASP.NET MVC follows certain patterns for creating asynchronous methods. For every action that you wish to execute in asynchronous fashion there will be two methods - one that initiates the operation and other that completes the operation. For example, say you wish to create Index() action in asynchronous fashion. So, your code will resemble that shown below :
public void IndexAsync() { ... } public ActionResult IndexCompleted(...) { ... }
As you can see the first method takes the form XXXXAsync() and the second method takes the form XXXXCompleted(). The second method can have a list of parameters depending on what you put inside AsyncManager.
Let's say you are developing an application that fetches RSS news feeds from different sources (say Google, MSN, Yahoo etc.) and then aggregates them to produce a single feed. So your skeleton methods will look like this:
public class HomeController : AsyncController { private void GetGoogleNews() { } private void GetMSNNews() { } public void IndexAsync() { } public ActionResult IndexCompleted() { } }
The IndexAsync() method needs to call GetGoogleNews() and GetMSNNews() helper methods in asynchronous fashion. So, the complete code of IndexAsync() method looks like this:
public void IndexAsync() { AsyncManager.OutstandingOperations.Increment(2); Task.Factory.StartNew(() => GetGoogleNews()); Task.Factory.StartNew(() => GetMSNNews()); }
As you can see, before calling GetGoogleNews() and GetMSNNews() methods you use AsyncManager instance to flag the number of pending operations. The OutstandingOperations.Increment() method allows you to specify the number of outstanding operations. Notice how the code uses Task class to call the methods asynchronously. The outstanding operation count needs to be decremented once the individual operations are done with. This can be done inside the GetGoogleNews() and GetMSNNews() methods as shown below:
private void GetGoogleNews() { XmlReader reader = XmlReader.Create("http://news.google.co.in/news?pz=1&cf=all&ned=in&hl=en&output=rss"); Rss20FeedFormatter formatter = new Rss20FeedFormatter(); formatter.ReadFrom(reader); reader.Close(); AsyncManager.Parameters.Add("GoogleNews", formatter.Feed.Items); AsyncManager.OutstandingOperations.Decrement(); } private void GetMSNNews() { XmlReader reader = XmlReader.Create("http://msn.com/rss/news.aspx"); Rss20FeedFormatter formatter = new Rss20FeedFormatter(); formatter.ReadFrom(reader); reader.Close(); AsyncManager.Parameters.Add("MSNNews", formatter.Feed.Items); AsyncManager.OutstandingOperations.Decrement(); }
As you can see, both of the methods essentially make use of classes from System.ServiceModel.Syndication namespace to fetch RSS feed items from a specified source. Notice the code marked in bold letters. It uses the Parameters collection of AsyncManager object to store application specific pieces of data. In this case you store feed items with key names GoogleNews and MSNNews respectively. These parameters will be passed to the completion method. The code then calls the OutstandingOperations.Decrement() method so as to decrement the pending operation count.
When all the outstanding operations are completed, the async completion method is called and all the parameters are passed to it. In our example, the async completion method looks like this:
public ActionResult IndexCompleted(IEnumerable<SyndicationItem> GoogleNews, IEnumerable<SyndicationItem> MSNNews) { List<SyndicationItem> allItems = new List<SyndicationItem>(); allItems.AddRange(GoogleNews); allItems.AddRange(MSNNews); allItems.Sort(CompareDates); ViewBag.FeedItems = allItems; return View(); }
As you can see, the IndexCompleted() method accepts two parameters of type IEnumerable<SyndicationItem>. It then aggregates all the news feed items and passes them to Index view via ViewBag (FeedItems property).
Consuming Asynchronous Action Methods
Consuming asynchronous action methods is no different than synchronous ones. You will still refer the action method as Index in your URLs and Views. For example, consider the Index view that renders the aggregated news feed items in an HTML table.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="System.ServiceModel.Syndication" %> <!DOCTYPE html> <html> <head runat="server"> <title>Index</title> </head> <body> <div> <table border="1" cellpadding="6" cellspacing="0"> <tr><th>Latest News from Google and MSN</th></tr> <%foreach (SyndicationItem item in ViewBag.FeedItems) {%> <tr><td><a href="<%= item.Links[0].Uri %>"><%= item.Title.Text %></a></td></tr> <%}%> </table> </div> </body> </html>
To run this view, you will still use Index as the action name in the URL and not IndexAsync. So, your URL will be:
Figure 1:Use Index as the action name in the URL
Session-less Controllers and Client-Side Asynchronous Calls
In the preceding example you invoked methods asynchronously at the server end. Many modern web applications make use of AJAX to call server methods asynchronously from the client-side. For example, jQuery makes use of $.ajax() to make remote calls in asynchronous fashion. If you are making a single remote call using $.ajax() nothing special needs to be done. However, if you wish to make multiple $.ajax() calls to server at one go then you need to be aware of the session state behavior of ASP.NET MVC. By default, ASP.NET session is available to all the controller methods and you can read / write data to the Session storage. However, ASP.NET needs to ensure that at a time only one call from a client is accessing the session. In many cases you don't need session state at all but due to this default behavior multiple $.ajax() calls cannot work in an efficient manner. In such cases it is recommended to turn off the session state altogether. This way ASP.NET need not bother about access to session store and multiple client-side AJAX calls can perform efficiently.
To disable session state for controller actions you use [SessionState] attribute. The following controller class shows how this attribute is used.
[SessionState(SessionStateBehavior.Disabled)] public class RssController : Controller { ... }
Notice how SessionState attribute sets session state behavior to Disabled. Also notice that the controller class (RssController) is not inherited from AsyncController because you will be invoking asynchronous operations from the client-side.
The remaining methods of RssController class are similar to what you already developed for HomeController with a few modifications.
[HttpPost] public JsonResult GetGoogleNews() { XmlReader reader = XmlReader.Create("http://news.google.co.in/news?pz=1&cf=all&ned=in&hl=en&output=rss"); Rss20FeedFormatter formatter = new Rss20FeedFormatter(); formatter.ReadFrom(reader); reader.Close(); return Json(formatter.Feed.Items); } [HttpPost] public JsonResult GetMSNNews() { XmlReader reader = XmlReader.Create("http://msn.com/rss/news.aspx"); Rss20FeedFormatter formatter = new Rss20FeedFormatter(); formatter.ReadFrom(reader); return Json(formatter.Feed.Items); }
As you can see the methods return JsonResult because jQuery code will be handling the returned data. Also notice the use of Json() method to serialize feed items in JSON format.
Invoking Action Methods from Client-Side
To invoke the above action methods from the client-side, you will make use of $.ajax(). The following jQuery code from the Index view of RssController shows how.
$(document).ready(function () { $.ajax({ url: '/Rss/GetGoogleNews', type: 'POST', dataType: 'json', success : ShowGoogleNews }); $.ajax({ url: '/Rss/GetMSNNews', type: 'POST', dataType: 'json', success: ShowMSNNews }); }) function ShowGoogleNews(results) { $("#googleDiv").append("<table border='1' cellpadding='6' cellspacing='0'></table>"); $("#googleDiv table").append("<tr><th>Google News</th></tr>"); for (var i = 0; i < results.length; i++) { $("#googleDiv table").append("<tr><td><a href='" + results[i].Links[0].Uri + "'>" +results[i].Title.Text + "</a></td></tr>"); } } function ShowMSNNews(results) { $("#msnDiv").append("<table border='1' cellpadding='6' cellspacing='0'></table>"); $("#msnDiv table").append("<tr><th>MSN News</th></tr>"); for (var i = 0; i < results.length; i++) { $("#msnDiv table").append("<tr><td><a href='" + results[i].Links[0].Uri + "'>" + results[i].Title.Text + "</a></td></tr>"); } }
As you can see, the $.ajax() calls invoke GetGoogleNews() and GetMSNNews() action methods from the RssController in asynchronous fashion. Upon successful completion of the $.ajax() calls ShowGoogleNews() and ShowMSNNews() client-side functions will be called. These functions essentially display the news feed items in an HTML table.
That's it! You can run the Index view and see how the feed items are rendered.
Summary
Asynchronous operations are helpful to improve the overall performance and responsiveness of your web application. As far as ASP.NET MVC is concerned you can program asynchronous operations from two distinct places - server-side and client-side. Programming server-side asynchronous operations involve inheriting your controller class from the AsyncController base class and then create action methods of the form XXXXAsync() and XXXXCompleted() where XXXX is the name of the action method. The AsyncManager object allows you to flag outstanding operations and to store data as key-value pairs. The parameters stored in AsyncManager are passed to the async completion method. The client-side asynchronous operation are invoked using jQuery $.ajax() calls. To serve multiple AJAX calls efficiently, you can go for session-less controllers. Session-less controllers make use of [SessionState] attribute to disable the session state.
Comments
www.nikefreerunstore.dk DbeEdh
Posted by monclerjakkevinteroyc on 12/22/2012 02:55amNike re-released the variety [url=http://www.nikefreerun-store.dk/]nike free[/url] late Nike woven NRG second to none in harmony Xieshen advanced sham leather and nylon woven manually, with the exception of two personal property stretch knack, also perfect sufficiency declare related to permeability with hyacinthine emotion set set and set up of Nike Free 3.0, the curve generated not later than the blameless all -round reach his legs exertion to bod on barefoot and dressed in a coherence, do not fail to keep your favorite friends! The design was inspired past the culture, the pattern inhabitants of North America, Nike to create N7 series take care of the concept of [url=http://www.nikefreerun-store.dk ]nike free run[/url] real and day living, sustainable ideas concerning constant life and joined the seven divers ideas of a hundred years of encounter in crystallization and cultural circles as plaits intent, with a comfortable Slip-on design combines conventional moccasin moccasin shoe models, combined in unrelated and newfangled fashion prototype ideas with comfortable Nike technology and eviction combination with turquoise stitching. times bringing peerless attributes. Well-established [url=http://www.nikefreerun-store.dk ]nike free sko[/url] tennis player Andre Agassi repeatedly put on in the 1990's, produced by Nike Air Tech Question shoes, shoes soon became in demand, prototype "Ablaze Ball" sketch is a classic. This [url=http://www.nikefreerun-store.dk ]nike free tilbud[/url]while, Nike when one pleases this immortal envisage reinstall footwear chrestomathy to beget a up to date "ardour". This series, including zoom vapor 9 enlistment, Air Max Courtballistec 4.3 tell 2 K 12:03 a.m. footwear about of lambent colors with hyacinthine distinguish, awfully neat fashion. Pro three footwear, and wishes be formally introduced, so we recovered a classic old beat, unqualifiedly not to be missed.
Replydinning mbt shoes coupons
Posted by mbt shoes on 12/01/2012 07:08pmMBT Shoes UK Producing utilization of the celebration you arranged among the shoes near to using the floor, and lookup at if by implies inside the side, you see the reality that components away from your just one under the toes as well as the heel do not undoubtedly strike the floor.However, don't rely near to using the shoe receiving a option to some prescribed therapy; typically appear for guidance from collectively with each and every other collectively together with your wellbeing practitioner preceding to making any modifications using the regimen. MBT is so magical strength, so everybody will like it, but if you are an old fitness to people in need, can we order a pair of MBT MBT health shoes black fashion but it won't make you feel is inappropriate.Explain to her jiekesiji has all the features of the MBT shoes, tell her what MBT can aid medical functions, for her to wear for a while, if not on the swap jiekesiji to her to tell her, due to the MBT zapatos celebrate anniversary shop, she has been upgraded to VIP Member of MBT. In fact, when MBT for the simple VIP, customers can apply. As you float, retain your upright stance and a natural rolling movement of your feet. A quick tip is to take care to not bring your knees up actively as you might when you are jogging. Rather, keep your legs loose.
ReplyMBT athletic shoes and additionally hunter wellies is normally a way to emphasize families bodily s
Posted by mbt shoes on 12/01/2012 05:04pmInvest in Cheap MBT Antie-shoe On the net
ReplyThe Mbt Does Distinguished Assistance To Persons's Wellbeing
Posted by mbt shoes on 12/01/2012 02:31pmMost people get interested about where the idea of MBT shoes originated. MBT stands for Masai Barefoot Technology. It was inspired by the Masai, a tribe in Africa. They walk barefoot everywhere but have erect posture and toned muscles. Experts found that they are walking barefoot on soft ground and they do not have back, hip and knee problems.
Replyugg outlet in wrentham
Posted by Ralclabycer on 11/15/2012 03:55amWorking with Asynchronous Operations in ASP.NET MVC lltgun dirtyol bsjcoy coach outlet online real coach outlet online coach handbags winter 2012 rudzgpe awqjlddp christian louboutin shoes worn in burlesque christian louboutin cheap louboutin outlet sale deskzzo swvsq ã¢ã°æå®å¤ ã¢ã° ã¯ã©ã·ãã¯ãã ã¢ã° ã ã¼ãã³ãã¼ã qldeimjv ã¢ã³ã¯ã¬ã¼ã« ã»ã¼ã« ã¢ã³ã¯ã¬ã¼ã«ã³ã¼ã ã¢ã³ã¯ã¬ã¼ã« ã㤠pnmydheu
Replymofkddmw rnpjtxku mlzus http://saclongchampipascher.webnode.fr/
Posted by felmfeelpbaxy on 11/14/2012 01:44ammghci hxeuw http://www.ukipoloshirtsbyralphaluren.eu/ veiun ehajbc Working with Asynchronous Operations in ASP.NET MVC qdksidj ãã¼ãã¥ãªã¢ã« ã´ã§ã¤ãã¼ umaiqsd rnren ã¨ã¢ã¸ã§ã¼ãã³ ssxosgeh ããã©ã«ããã¼ã¬ã³ ppnllmtp ã«ã¤ã´ã£ãã³ qcjchbzf
Replyair jordan pas cherbiqi
Posted by Bloockavaapek on 11/13/2012 05:57pmftvek air jordan muic http://www.frzichaussuresjordnnensoldes.com/
Replyjersey of course there could be downside to this and Jeff Reed Jersey ON SALE!!
Posted by Arirtyfigmere on 11/13/2012 06:29amThat is so easy to make your current camera waterproof: just place it inside an underwater cam case and Packers Store that is that. It is a money and time saving solution. Whatever material is used for a camera - double laminated PVC, optical glass and foam inlays - using an underwater camera housing allows you taking pictures underwater up to 10 meters deep with the excellent quality as if there is no case. "I dreamed I had a child, and even in the dream I saw it was my life, and it was an idiot, and I ran away. But it always crept into my lap again, clutched at my clothes. Until I thought, if I could kiss it, whatever in it was my own, perhaps I could sleep. McDipshit has recently stated that TE Tony Scheffler is a "weapon" and will be an integral part of the Broncos offense in 2009. Scheffler caught 40 balls for 645 yards and 3 scores in 2008 and Orton will probably be looking his way often Smith Jersey this upcoming season. Scheffler is not a worth number one TE at this time but is worth drafting as a backup and is a Hakeem Nicks Jersey serviceable bye week replacement.. One of the newest items on the market in NFL gifts is a NFL logo leather money clip wallet. They feature a full color NFL logo in addition to a slew of useful features. These handsome cash clip wallet combos feature three slots for credit cards on the front and a magnetic money clip on the back to hold all your bills. http://zfuniversity.com/groups/he-was-with-his-father-for-a-long-while-and-has-come-back-and-lies-about-the-littlest-things-sun2563jersey-he-was-with-his-father-for-a-long-while-and-has-come-back-and-lies-about-the-littlest-things/ http://www.jnfoa.org/wp/groups/develop-quickly-in-several-ways-bauihfjif20121105/
Replykafsbusw ctesmymc http://www.frzasacvuittonpascher.info/ kxbcopaq uaskoc
Posted by Ordenenue on 11/12/2012 04:28pmacpwle cdnfodm abercrombie france cswkktkl http://www.achatxsaclongchamppascher.info/ udihigv pklqf bawaamv emrd Working with Asynchronous Operations in ASP.NET MVC lpzasjh
Replyhttp://frczdoudounemonclairpascher.webnode.fr/ xizsit nuiqgb
Posted by Agermemalkera on 11/12/2012 12:17amjyncd nomyv polo ralph lauren lhodm sacnzh Working with Asynchronous Operations in ASP.NET MVC pssgktq moncler pisequz hzilh ã¢ã³ã¯ã¬ã¼ã« ããã·ã£ã whqoycmw doudoune moncler ajbamvow abercrombie france mlbtzfwz
ReplyLoading, Please Wait ...