ASP.NET MVC何时使用异步Action

在没有使用异步Action以前,在Action内,好比有以下的写法:编程

 

public ActionResult Index()
{
    CustomerHelper cHelper = new CustomerHelper();
    List<Customer> result = cHelper.GetCustomerData();
    return View(result);
}

 

以上,假设,GetCustomerData方法是调用第三方的服务,整个过程都是同步的,大体是:框架

 

→请求来到Index这个Action
→ASP.NET从线程池中抓取一个线程
→执行GetCustomerData方法调用第三方服务,假设持续8秒钟的时间,执行完毕
→渲染Index视图异步

 

在执行执行GetCustomerData方法的时候,因为是同步的,这时候没法再从线程池抓取其它线程,只能等到GetCustomerData方法执行完毕。async

 

这时候,能够改善一下整个过程。异步编程

→请求来到Index这个Action
→ASP.NET从线程池中抓取一个线程服务于Index这个Action方法
→同时,ASP.NET又从线程池中抓取一个线程服务于GetCustomerData方法
→渲染Index视图,同时获取GetCustomerData方法返回的数据spa

 

因此,当涉及到多种请求,好比,一方面是来自客户的请求,一方面须要请求第三方的服务或API,能够考虑使用异步Action。线程

 

假设有这样的一个View Model:设计

 

public class Customer
{
    public int Id{get;set;}
    public Name{get;set;}
}

 

假设使用Entity Framework做为ORM框架。get

 

public class CustomerHelper
{
	public async Task<List<Customer>> GetCustomerDataAsync()
	{
		MyContenxt db = new MyContext();
		var query = from c in db.Customers
					orderby c.Id ascending
					select c;
		List<Customer>	result = awai query.ToListAsycn();
		return result;				
	}
}

 

如今就能够写一个异步Action了。同步

 

public async Task<ActionResult> Index()
{
	CustomerHelper cHelper = new CustomerHelper();
	List<Customer> result = await cHlper.GetCustomerDataAsync();
	return View(result);
}

 

Index视图和同步的时候相比,并无什么区别。

 

@model List<Customer>
@foreach(var customer in Model)
{
	<span>@customer.Name</span>
}

 

固然,异步还设计到一个操做超时,默认的是45秒,但能够经过AsyncTimeout特性来设置。

 

[AsyncTimeout(3000)]
public async Task<ActionResult> Index()
{
	...
}

 

若是不想对操做超时设限。

 

[NoAsyncTimeout]
public async Task<ActionResult> Index()
{
	...
}

 

 

综上,当涉及到调用第三方服务的时候,就能够考虑使用异步Action。async和await是异步编程的2个关键字,async总和Action成对出现,而在调用异步方法以前要加上await关键字。

相关文章
相关标签/搜索