[英]Async/Await and multi-layer web application
Because of some optimalization issues, i decided to rewrite controller's action method to asynchronous one. 由于一些优化问题,我决定将控制器的操作方法重写为异步方法。 It is a multi-layer application, and since that, i'm facing an architecture problem, and i wonder what are the main differences between two approches I will show below.
它是一个多层应用程序,因此,我面临一个体系结构问题,我想知道下面将显示的两个方法之间的主要区别是什么。 Let's say my synchronous method is:
假设我的同步方法是:
public virtual ActionResult Method()
{
SomeLogic.LargeOperation();
return View(...);
}
LargeOpertation does a lot. LargeOpertation做很多事情。 Here's pseudocode:
这是伪代码:
public void LargeOpertion() {
DatabaseManipulations1();
IndependentWork();
CallingToWebService1();
IndependentWork();
DatabaseManipulations2();
IndependentWork();
CallingToWebService2();
IndependentWork();
}
Each method inside LargeOperations has couple methods inside itself, and so on ... The question is: Do I need to make them all async and use await in almost every of application layer? LargeOperations内部的每个方法内部都有几个方法,依此类推……问题是:我是否需要使它们全部异步并在几乎每个应用程序层中使用await?
public virtual Task<ActionResult> Method()
{
await SomeLogic.LargeOperation();
return View(...);
}
public async Task LargeOpertion() {
await DatabaseManipulations1();
IndependentWork();
await CallingToWebService1();
IndependentWork();
await DatabaseManipulations2();
IndependentWork();
await CallingToWebService2();
IndependentWork();
}
Or can I just use task on LargeOpertaion like that: 或者我可以像这样在LargeOpertaion上使用任务:
public virtual Task<ActionResult> Method()
{
await Task.Run(() => SomeLogic.LargeOperation());
return View(...);
}
Let's also assume that IndependentWork() is not so large. 我们还假设IndependentWork()不太大。
Because of some optimalization issues, i decided to rewrite controller's action method to asynchronous one.
由于一些优化问题,我决定将控制器的操作方法重写为异步方法。
Before you start, you should realize what async
will do for you and what it won't . 在开始之前,您应该意识到
async
将为您带来什么,而不会给您带来什么 。
Asynchronous operations do not run faster. 异步操作不会更快地运行。 So, an asynchronous call to the database is not any faster than a synchronous call to the database.
因此,对数据库的异步调用不会比对数据库的同步调用更快。
await
doesn't return to the browser early. await
不会尽早返回浏览器。 All it does is release the current thread back to the ASP.NET threadpool. 它所做的只是将当前线程释放回ASP.NET线程池。
So, each individual request still takes the same total amount of time (actually, just slightly longer , but not by a detectable amount). 因此,每个单独的请求仍会花费相同的总时间(实际上,只是稍长一些 ,但不是可检测的时间)。
What async
does help with is scalability - that is, the ability of your application to handle more requests with the same resources. async
对可伸缩性的帮助在于可伸缩性,即应用程序使用相同资源处理更多请求的能力。 However, it only helps the scalability of your web app - it can't magically reach into your database and make that scale. 然而,它不仅可以帮助你的web应用程序的可扩展性-它不能达到神奇到你的数据库,并作出这样的规模。 So, if your scalability bottleneck is your database and not ASP.NET (which is usually the case), then
async
won't help you at all. 因此,如果您的可伸缩性瓶颈是数据库而不是ASP.NET(通常是这种情况),那么
async
将完全无法帮助您。
If your database backend is scalable (eg, NoSQL, Azure SQL, or a database cluster), and if your ASP.NET app needs to scale up, then you can benefit from async
. 如果你的数据库后端是可扩展的(例如,NoSQL的,Azure的SQL或数据库集群), 如果你的ASP.NET应用程序需要扩大规模,那么你就可以受益于
async
。
Do I need to make them all async and use await in almost every of application layer?
我是否需要使它们全部异步并在几乎每个应用程序层中使用await?
The best approach is to start at the "leaves" - the lowest-level methods - and work your way up from there. 最好的方法是从“叶子”(最低层的方法)开始,然后从那里开始。 In this case, start at the database interaction and convert those to
async
first. 在这种情况下,请从数据库交互开始,然后将其首先转换为
async
。
However, at no point should you use Task.Run
or Task.Factory.StartNew
. 但是,绝对不要使用
Task.Run
或Task.Factory.StartNew
。 Instead, use true asynchronous APIs, such as the async
support in EF6. 相反,请使用真正的异步API,例如EF6中的
async
支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.