mvc4 Asynchronous
英文原文:http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4</>
An asynchronous request takes the same amount of time to process as a synchronous request. For example, if a request makes a web service call that requires two seconds to complete, the request takes two seconds whether it is performed synchronously or asynchronously. However, during an asynchronous call, a thread is not blocked from responding to other requests while it waits for the first request to complete. Therefore, asynchronous requests prevent request queuing and thread pool growth when there are many concurrent requests that invoke long-running operations.
In general, use synchronous methods for the following conditions:
- The operations are simple or short-running.
- Simplicity is more important than efficiency.
- The operations are primarily CPU operations instead of operations that involve extensive disk or network overhead. Using asynchronous action methods on CPU-bound operations provides no benefits and results in more overhead.
In general, use asynchronous methods for the following conditions:
- You're calling services that can be consumed through asynchronous methods, and you're using .NET 4.5 or higher.
- The operations are network-bound or I/O-bound instead of CPU-bound.
- Parallelism is more important than simplicity of code.
- You want to provide a mechanism that lets users cancel a long-running request.
- When the benefit of switching threads out weights the cost of the context switch. In general, you should make a method asynchronous if the synchronous method waits on the ASP.NET request thread while doing no work. By making the call asynchronous, the ASP.NET request thread is not stalled doing no work while it waits for the web service request to complete.
- Testing shows that the blocking operations are a bottleneck in site performance and that IIS can service more requests by using asynchronous methods for these blocking calls.
The downloadable sample shows how to use asynchronous action methods effectively. The sample provided was designed to provide a simple demonstration of asynchronous programming in ASP.NET MVC 4 using .NET 4.5. The sample is not intended to be a reference architecture for asynchronous programming in ASP.NET MVC. The sample program calls ASP.NET Web API methods which in turn call Task.Delay to simulate long-running web service calls. Most production applications will not show such obvious benefits to using asynchronous action methods.
public async Task<ActionResult> GizmosAsync() { ViewBag.SyncOrAsync = "Asynchronous"; var gizmoService = new GizmoService(); return View("Gizmos", await gizmoService.GetGizmosAsync()); }
public async Task<List<Gizmo>> GetGizmosAsync() { var uri = Util.getServiceUri("Gizmos"); using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.GetAsync(uri); return (await response.Content.ReadAsAsync<List<Gizmo>>()); } }
Performing Multiple Operations in Parallel
public async Task<ActionResult> PWGasync() { ViewBag.SyncType = "Asynchronous"; var widgetService = new WidgetService(); var prodService = new ProductService(); var gizmoService = new GizmoService(); var widgetTask = widgetService.GetWidgetsAsync(); var prodTask = prodService.GetProductsAsync(); var gizmoTask = gizmoService.GetGizmosAsync(); await Task.WhenAll(widgetTask, prodTask, gizmoTask); var pwgVM = new ProdGizWidgetVM( widgetTask.Result, prodTask.Result, gizmoTask.Result ); return View("PWG", pwgVM); }
Using a Cancellation Token
Asynchronous action methods returning Task<ActionResult>are cancelable, that is they take a CancellationToken parameter when one is provided with the AsyncTimeout attribute. The following code shows the GizmosCancelAsync method with a timeout of 150 milliseconds.
[AsyncTimeout(150)] [HandleError(ExceptionType = typeof(TimeoutException), View = "TimeoutError")] public async Task<ActionResult> GizmosCancelAsync( CancellationToken cancellationToken ) { ViewBag.SyncOrAsync = "Asynchronous"; var gizmoService = new GizmoService(); return View("Gizmos", await gizmoService.GetGizmosAsync(cancellationToken)); }
public async Task<List<Gizmo>> GetGizmosAsync(string uri, CancellationToken cancelToken = default(CancellationToken)) { using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.GetAsync(uri, cancelToken); return (await response.Content.ReadAsAsync<List<Gizmo>>()); } }
“麻烦”是自己“处理”不当的结果
“困难”是自己“学习”不够的反射
“挫折”是自己“努力”不足的代价
浙公网安备 33010602011771号