Using an asynchronous controller in ASP. NET MVC

Source: Internet
Author: User

Thread pool

have been trying to rewrite the project into asynchronous, but the asp.netmvc3 is too cumbersome to write,. NET 4.5 and the code is easier to write, Ms seems to have been like this, every mature thing, must evolve several versions, to tend to standardize. Why is it necessary to use async in ASP. IIS has a thread pool to handle the user's request, and when a new request comes in, the thread in the pool is dispatched to handle the request, however, when the concurrency is high, the threads in the pool are not able to meet so many requests. Each thread in the pool is in a busy state when the request is processed and the thread that processes the request is blocked, and the thread cannot service another request, and if the request queue is full, the WEB server rejects the request and is in an HTTP 503 busy state. If you are dealing with some high latency, such as a network operation, most of these threads just wait for the state to do nothing for most of the time, so that the thread can use asynchronous programming to make better use of it.

Asynchronous processing

For example , if a request generates a network call that takes two seconds to complete, it takes two seconds for the request to execute synchronously or asynchronously. However, during an asynchronous call, the server does not block the response to other requests while waiting for the first request to complete. Therefore, when there are many requests to invoke a long-running operation, an asynchronous request can prevent the request from queuing up. The maximum thread pool in. NET 4.5 has also added await and async keywords in the. NET 4.5 to simplify asynchronous programming.

Synchronous or asynchronous (excerpt from MSDN)

Typically, you use a synchronization pipeline when the following conditions are true:

    • The operation is simple or has a short running time.

    • Simplicity is more important than efficiency.

    • This operation is primarily a CPU operation rather than an operation that contains a large amount of disk or network overhead. The use of asynchronous operation methods on CPU bound operations does not provide any benefit and also results in more overhead.

Typically, you use an asynchronous pipeline when the following conditions are true:

    • Operations are network-bound or I/O-bound rather than CPU-bound.

    • Testing shows that blocking operations are a bottleneck for site performance and that IIS can service more requests by using asynchronous action methods for these blocking calls.

    • Parallelism is more important than the simplicity of the code.

    • You want to provide a mechanism for users to cancel long-running requests.

Using asynchronous in ASP.

        //Get:async[AsyncTimeout ( +)]         Public AsyncTask<actionresult>Index () {vardata =awaitGetpagetaskasync ("http://163.com"); stringOtherdata ="Irving"; returndata; }        /// <summary>        ///Handling Asynchronous Requests/// </summary>        /// <param name= "url" ></param>        /// <returns></returns>         Public AsyncTask<actionresult> Getpagetaskasync (stringURL) {            Try            {                using(varClient =NewHttpClient ()) {                    awaitTask.delay ( the); varFetchtexttask =client.                    Getstringasync (URL); returnJson (New{Fetchtext=awaitFetchtexttask, Error="NO"}, Jsonrequestbehavior.allowget); }            }            Catch(WebException exception) {Throwexception; //todo:logging, UPDATE statistics etc.            }        }    }

Refer:
The Managed Thread Pool
http://msdn.microsoft.com/zh-cn/library/0ka9477y.aspx
Using a asynchronous Controller in ASP. NET MVC
Http://msdn.microsoft.com/zh-cn/library/ee728598%28v=vs.100%29. aspx
Tips and Tricks on what to improve MVC application performance
Http://www.robertsindall.co.uk/blog/how-to-imp rove-mvc-application-performance/
Using asynchronous Methods in ASP 4
Http://www.asp.net/mvc/tutorials /mvc-4/using-asynchronous-methods-in-aspnet-mvc-4
Asynchronous Action methods in MVC 4
/http Www.codeproject.com/Tips/573154/Asynchronous-Action-Methods-in-MVC-4 the definition and execution principle of asynchronous Action under

ASP. NET MVC
http://www.cnblogs.com/artech/archive/2012/06/20/async-action-in-mvc.html

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.