How to use an asynchronous controller in asp.net MVC

Source: Internet
Author: User
Tags httpcontext net thread

I have a few points of interest when using asynchronous operations in ASP.net mvc. When does asynchronous operation improve the performance of my application and when did it not improve?

Is it really good to use asynchronous operations everywhere in asp.net mvc?
For the Awaitable method: Should you use the ASYNC/AWAIT keyword when querying the database (through ef/bhibernate/other ORM)?
How many times can you use the AWAIT keyword to query the database asynchronously in a separate action method?

The asynchronous action method is useful when an action must perform multiple independent long-running operations.

Suppose I have three operations that take 500, 600, and 700 milliseconds, respectively. With a synchronous call, the total response time will be slightly more than 1800 milliseconds. However, if it is an asynchronous invocation (concurrency), the total response time will be slightly more than 700 milliseconds, because that is the duration of the longest task/operation.

A classic use of asynchronous controller classes is for long-running Web service invocations.

Should the database be called asynchronously?

The IIS thread pool can often handle more blocking requests than one database server. If the database encounters a bottleneck, an asynchronous call does not speed up the response of the database. Because there is no current limiting mechanism, the use of asynchronous calls to effectively distribute more tasks to a helpless database server will only transfer more burden to the database. If your database encounters a bottleneck, the asynchronous call is not a magic bullet.

Also, asynchrony does not imply concurrency. Asynchronous execution frees up a valuable thread pool thread that is blocked by external resources without complexity or performance loss. This means that the same IIS machine can handle more concurrent requests, rather than it will run faster.

You can look at this article on MSDN. The author spends a lot of effort on this blog to describe when to use async in asp.net rather than how to use it.

answer title question:

First, understand that async/await is the root of freeing threads. In GUI application, the main release GUI thread, so the user experience is better. In server applications (including ASP.net MVC), the request thread is primarily freed, so the server can be extended.

In particular, it will not:

Make your personal requests complete faster. In fact, they will finish (only a little bit) more slowly. When a await is encountered, it is returned to the caller/browser. Await will only "yield" to the asp.net thread pool, not the browser.

Answer question 1:

When you are going to do I/O, I can say that it is good to use asynchronous operations everywhere. Although it may not necessarily be beneficial (see below).

However, it is not good to use asynchronous operations for CPU-constrained methods. Sometimes developers think it's a scary idea to get the benefits of async by invoking Task.run in a controller. Because that code does not benefit (in fact, they consume the overhead of extra thread switching) by opening other threads to end the release of the request thread.

Answer question 2:

You can use any method you can wait for. If your ORM does not support async, then don't try to wrap it in task.run or anything similar to that.

Notice what I mean by "you can use". If you're talking about a single database of ASP.net MVC, then (basically sure) you don't get any scalability benefits from Async. This is because IIS can handle more concurrent requests than a single instance of SQL Server (or other classic RDBMS). However, if your backend is a more modern--sql server cluster, Azure SQL, NoSQL, and so on-your back-end scalability and scalability bottlenecks are IIS, then you can gain scalability benefits from Async.

Answer question 3:

How many times you love to use, as long as you like. However, note that many ORM have the principle of "one operation at a time." In particular, EF each dbcontext only allows a single operation, which is true whether the operation is synchronous or asynchronous.

And again, remember the scalability of your back end. If you encounter a single instance of SQL Server, and IIS can already put SQL Server on full load, SQL Server double or three times times the pressure is not good for you.



Using asynchronous controllers in ASP.net MVC

How the thread pool handles requests

On a WEB server, the. NET Framework maintains a thread pool for service asp.net requests. When the request arrives, the thread in the pool is dispatched to handle the request. If the request is synchronized, the thread that handles the request is blocked when the request is processed, and the thread cannot service another request.

This may not be a problem because the thread pool can be set large enough to hold many blocked threads. However, the number of threads in the thread pool is limited. In large applications that handle multiple long-running requests at the same time, all available threads may be blocked. This situation is called "thread insufficiency." When this happens, the WEB server queues the request. If the request queue is full, the WEB server rejects the request and is in the HTTP 503 state (the server is too busy).

Processing asynchronous requests

In applications where threads are likely to be low, you can configure operations to be handled asynchronously. The asynchronous request is the same as the processing time required for the synchronization request. For example, if a request generates a network call that takes two seconds to complete, it takes two seconds for the request to be executed either 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 long-running operations, asynchronous requests can prevent requests from queuing up.

When an asynchronous operation is invoked, the following steps are performed:

The WEB server obtains a thread from the thread pool (the worker thread) and schedules it to handle incoming requests. This worker thread initiates an asynchronous operation.

This worker thread is returned to the thread pool to service another WEB request.

Notifies the ASP.net when an asynchronous operation completes.

The WEB server obtains a thread from the thread pool (possibly a different thread from the thread that initiates the asynchronous operation) to handle the remainder of the request, including rendering the response.

The following illustration shows the asynchronous Pattern.




Select a Sync action method or asynchronous action method

This section lists guidelines for when to use synchronous or asynchronous action methods. This is just a few guidelines; You must examine each application individually to determine whether the asynchronous operation method can help improve performance.

Typically, the synchronization pipeline is used when the following conditions are true:

The operation is simple or runs for a short 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. Using asynchronous action methods for CPU-bound operations does not provide any benefit and also results in additional overhead.

Typically, an asynchronous pipeline is used 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 Web site performance and that IIS can service more requests by using asynchronous action methods on these blocking calls.

Parallelism is more important than the simplicity of the code.

You want to provide a mechanism that allows users to cancel long-running requests.

The downloaded example shows how to use the asynchronous action method effectively. The example program calls the sleep method to simulate a long-running process. Few product applications will show the benefits of using asynchronous operations in such an obvious way.

You should test your application to determine whether asynchronous methods provide performance benefits. In some cases, it may be better to increase the maximum number of concurrent requests for IIS per CPU and the maximum number of concurrent threads per CPU. For more information about asp.net thread configuration, see the article on Thomas Marquardt's blog asp.net thread Usage on IIS 7.0 and 6.0 (ASP.net thread usage on IIS 7.0 and 6.0) )。 For more information about when to perform asynchronous database calls, see the article on the Rick Anderson blog Should My db calls be asynchronous? (should my database calls be asynchronous?) )。

Few applications require that all methods of operation be asynchronous. Typically, converting a small number of synchronous operations methods to asynchronous methods can significantly increase the amount of work required.


to convert a synchronous action method to an asynchronous action method

The following code example demonstrates a synchronous action method that displays a news item from a portal site controller. Request Portal/news?city=seattle to display Seattle news.

C#

public class Portalcontroller:controller {
Public ActionResult News (string city) {
Newsservice newsservice = new Newsservice ();
Viewstringmodel headlines = newsservice.getheadlines (city);
Return View (Headlines);
}
}

The following example shows a News action method that is rewritten as an asynchronous method.

C#

public class Portalcontroller:asynccontroller {
public void Newsasync (string city) {

AsyncManager.OutstandingOperations.Increment ();
Newsservice newsservice = new Newsservice ();
newsservice.getheadlinescompleted + = (sender, E) =>
{
asyncmanager.parameters["Headlines"] = E.value;
AsyncManager.OutstandingOperations.Decrement ();
};
Newsservice.getheadlinesasync (city);
}

Public ActionResult newscompleted (string[] headlines) {
Return View (News, New Viewstringmodel
{
Newsheadlines = Headlines
});
}
}

Converting a synchronous action method to an asynchronous action method consists of the following steps:

Do not derive the controller from the Controller, but from the Asynccontroller. A controller derived from Asynccontroller enables ASP.net to handle asynchronous requests, and these controllers can still provide services for synchronous operation methods.

Create two methods for the operation. The method that initiates the asynchronous process must have a name consisting of the action and suffix "Async". The method called when the asynchronous process completes (the callback method) must have a name consisting of the action and suffix "Completed". In the previous example, the News method has been converted to two methods: Newsasync and newscompleted.

The Newsasync method returns void (there is no value in Visual Basic). The Newscompleted method returns the ActionResult instance. Although the operation consists of two methods, it is accessed using the same URL as the synchronous action method (for example, Portal/news?city=seattle). Other methods, such as Redirecttoaction and Renderaction, will also refer to the action method according to News rather than newsasync.

Parameters passed to newsasync use the normal parameter binding mechanism. Arguments passed to newscompleted use the Parameters dictionary.

Replaces a synchronous call in the original ActionResult method with an asynchronous call in an asynchronous action method. In the example above, a call to Newsservice.getheadlines is replaced with a call to Newsservice.getheadlinesasync.

The Newsservice class used by the Newsasync method is an example of a service that exposes a method using an event-based asynchronous Pattern. For more information about this pattern, see Overview of event-based asynchronous patterns.

The Outstandingoperations property notifies asp.net how many operations have been suspended. This is necessary because ASP.net cannot determine how many operations were initiated by the action method or when these operations were completed. When the Outstandingoperations property is zero, asp.net can complete the asynchronous operation by calling the Newscompleted method.

Note the following about some of the things that you do about asynchronous operation methods:

If the action name is Sample, the framework looks for the SampleAsync and samplecompleted methods.

The view page should be named Sample.aspx, not named Sampleasync.aspx or samplecompleted.aspx. (The operation name is Sample, not sampleasync.) )

The controller cannot contain an asynchronous method named SampleAsync and a synchronization method named Sample. If both methods are included, a AmbiguousMatchException exception is thrown because the SampleAsync action method and the Sample action method have the same request signature.

perform multiple operations in parallel

Asynchronous operation methods are useful when an operation must perform several separate operations. For example, a portal site might not only show news, but also show sports, weather, stocks, and other information.

The following example shows a synchronized version of the news portal site Index action method.

C#

Public ActionResult indexsynchronous (string city) {

Newsservice newsservice = new Newsservice ();
string[] Headlines = Newsservice.getheadlines ();

Sportsservice sportsservice = new Sportsservice ();
String[] scores = Sportsservice.getscores ();

WeatherService WeatherService = new WeatherService ();
string[] Forecast = Weatherservice.getforecast ();

Return View ("Common", new Portalviewmodel {
Newsheadlines = Headlines,
Sportsscores = scores,
Weather = Forecast
});
}

Executes a call to each service sequentially. Therefore, the time required to respond to a request is the sum of the time each service invocation plus a small amount of system overhead. For example, if each call is 400, 500, and 600 milliseconds, the total response time will be slightly greater than 1.5 seconds. However, if the service invocation (in parallel) is performed asynchronously, the total response time will be slightly greater than 600 milliseconds, because this is the duration of the longest task.

The following example shows an asynchronous version of the news portal site Index action method.

C#

public void Indexasync (string city) {
AsyncManager.OutstandingOperations.Increment (3);

Newsservice newsservice = new Newsservice ();
newsservice.getheadlinescompleted + = (sender, E) =>
{
asyncmanager.parameters["Headlines"] = E.value;
AsyncManager.OutstandingOperations.Decrement ();
};
Newsservice.getheadlinesasync ();

Sportsservice sportsservice = new Sportsservice ();
sportsservice.getscorescompleted + = (sender, E) =>
{
Asyncmanager.parameters["scores"] = E.value;
AsyncManager.OutstandingOperations.Decrement ();
};
Sportsservice.getscoresasync ();

WeatherService WeatherService = new WeatherService ();
weatherservice.getforecastcompleted + = (sender, E) =>
{
asyncmanager.parameters["forecast"] = E.value;
AsyncManager.OutstandingOperations.Decrement ();
};
Weatherservice.getforecastasync ();
}

Public ActionResult indexcompleted (string[] headlines, string[] scores, string[] forecast) {
Return View ("Common", new Portalviewmodel {
Newsheadlines = Headlines,
Sportsscores = scores,
Weather = Forecast
});
}
}

In the previous example, the Increment method was invoked with parameter 3 because there were three asynchronous operations.

To add an attribute to an asynchronous action method

If you want to apply an attribute to an asynchronous action method, apply them to the Actionasync method instead of the Actioncompleted method. Ignores attributes on the Actioncompleted method.

Two new features have been added: Asynctimeoutattribute and Noasynctimeoutattribute. These features allow you to control the asynchronous timeout period.

using Beginmethod/endmethod mode

If the asynchronous action method invokes a service that exposes a method using the Beginmethod/endmethod mode, the callback method (the method that is passed as an asynchronous callback parameter to the Begin method) may execute on a thread that is not controlled by the asp.net. In this case, HttpContext.Current will be null and race conditions may appear when the application accesses members of the Asyncmanager class, such as Parameters. To ensure that you have access to the HttpContext.Current instance and avoid race conditions, you can restore httpcontext.current by calling Sync () from the callback method.

If the callback completes synchronously, the callback is executed on the thread controlled by ASP.net and the operation is serialized, so no concurrency problem occurs. The call to Sync () from a thread already controlled by asp.net has undefined behavior.

The Actioncompleted method is always invoked on threads controlled by ASP.net. Therefore, do not invoke Sync () from this method.

Callbacks passed to the Begin method may be invoked using threads that are controlled by ASP.net. Therefore, you must check for this condition before calling Sync (). If the operation is completed synchronously (that is, if completedsynchronously is true), the callback is executed on the original thread, and you do not have to invoke Sync (). If the operation is completed asynchronously (that is, completedsynchronously to false), the callback is performed on the online pool or I/O completion port thread, and you must Sync ().

For more information about Beginmethod/endmethod mode, see the overview of asynchronous programming and the article on the blog of Rick Anderson Using the Beginmethod/endmethod pattern with MVC (for MVC In Beginmethod/endmethod mode).


Class reference

The following table lists the key classes for asynchronous action methods.

Class description

Asynccontroller provides a base class for asynchronous controllers.

Asyncmanager provides asynchronous operations for the Asynccontroller class.

Related Article

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.