F # and ASP.net (1): event-based Asynchronous Pattern and asynchronous action

Source: Internet
Author: User

One of the most effective ways to improve the scalability of ASP.net applications is to use asynchronous requests. The asynchronous action is not directly supported in ASP.net MVC 1, so we need to use some simple hack ways to do this. However, simple hack cannot take advantage of the full functionality of ASP.net mvc, but fortunately ASP.net MVC 2 has officially supported asynchronous request processing in asp.net and is available to developers in a more user-friendly way. Unfortunately, because of language constraints, this use of the way is still somewhat inconvenient, and this time is the application of F #.

Event-based Asynchronous mode

Speaking of. NET in the asynchronous programming model,. NET programmer is the most familiar should be the Begin/end method. For example, in the WebRequest class, there is a pair of methods:

var request = WebRequest.Create("http://blog.zhaojie.me/");
request.BeginGetResponse(ar =>
{
   var response = request.EndGetResponse(ar);
   // use the response object

}, null);

After the BeginGetResponse method of the WebRequest object is invoked, the current calling thread is not blocked, and after the asynchronous operation completes, a callback function is called (that is, the code constructed using the lambda expression) is notified, Call the EndGetResponse method in this callback function to get a WebResponse object as the result. In this asynchronous operation, because of the great IOCP, we can launch thousands of connections at the same time with very few threads (no exaggeration, I used to do comet experiments in IIS, and set up over 2w connections to communicate).

But, in fact, in. NET also has an event-based asynchronous Pattern (event-based asynchronous Pattern,eap), which has been well recognized in the CLR via C # (3rd Edition) (Incidentally, I still have dozens of pages to read, and there are quite a lot of updates that are worth seeing. One of the typical cases of event-based asynchronous programming is the WebClient class:

var client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
   var html = args.Result;
   // ...
};

client.DownloadStringAsync(new Uri("http://blog.zhaojie.me/"));

The key to an event-based asynchronous pattern is that it uses events as a notification mechanism at the end of work. It differs markedly from the Begin/end asynchronous model. For example, when an error occurs, an exception is thrown for the begin/end model at the end method call, and for an event-based asynchronous Pattern, it uses the exception property of the event argument to tell the programmer if an exception has occurred. If the exception property is null, all is OK, otherwise it returns the exception that occurred during the asynchronous call.

Using asynchronous action in ASP.net MVC

My hack used the Begin/end asynchronous programming model, and ASP.net MVC 2 used an event-based asynchronous Pattern. Around this pattern, the ASP.net MVC Asynccontroller also provides related helper methods, making it relatively easy to write asynchronous action. Here I refer directly to the example on MSDN to illustrate the problem. First, we prepare an ordinary sync action:

public class PortalController : Controller
{
   public ActionResult News(string city)
   {
     var newsService = new NewsService();
     var headlines = newsService.GetHeadlines(city);
     return View(headlines);
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.