什么时候应该在ASP中使用异步控制器。净MVC吗?

时间:2022-09-11 10:31:52

I have some concerns using async actions in ASP.NET MVC. When does it improve performance of my apps, and when does it not?

在ASP中使用异步操作有一些问题。净MVC。什么时候能提高我的应用程序的性能,什么时候不能?

  1. Is it good to use async action everywhere in ASP.NET MVC?
  2. 在ASP中到处使用异步操作是否好?净MVC吗?
  3. Regarding awaitable methods: shall I use async/await keywords when I want to query a database (via EF/NHibernate/other ORM)?
  4. 关于可爱的方法:当我想查询数据库(通过EF/NHibernate/其他ORM)时,我是否应该使用异步/等待关键字?
  5. How many times can I use await keywords to query the database asynchronously in one single action method?
  6. 我可以使用等待关键字在一个操作方法中异步查询数据库多少次?

6 个解决方案

#1


85  

Asynchronous action methods are useful when an action must perform several independent long running operations.

当一个操作必须执行几个独立的长运行操作时,异步操作方法是有用的。

A typical use for the AsyncController class is long-running Web service calls.

AsyncController类的一个典型用法是长时间运行的Web服务调用。

Should my database calls be asynchronous ?

我的数据库调用应该是异步的吗?

The IIS thread pool can often handle many more simultaneous blocking requests than a database server. If the database is the bottleneck, asynchronous calls will not speed up the database response. Without a throttling mechanism, efficiently dispatching more work to an overwhelmed database server by using asynchronous calls merely shifts more of the burden to the database. If your DB is the bottleneck, asynchronous calls won’t be the magic bullet.

IIS线程池通常可以处理比数据库服务器更多的同时阻塞请求。如果数据库是瓶颈,异步调用不会加快数据库响应。如果没有节流机制,通过使用异步调用有效地将更多的工作分派给一个不堪重负的数据库服务器,只会将更多的负担转移到数据库中。如果您的DB是瓶颈,异步调用就不是灵丹妙药。

You should have a look at 1 and 2 references

你应该看看1和2个参考文献

Derived from @PanagiotisKanavos comments:

来自@PanagiotisKanavos评论:

Moreover, async doesn't mean parallel. Asynchronous execution frees a valuable threadpool thread from blocking for an external resource, for no complexity or performance cost. This means the same IIS machine can handle more concurrent requests, not that it will run faster.

而且,异步并不意味着并行。异步执行使有价值的threadpool线程不必为外部资源阻塞,而无需付出任何复杂性或性能成本。这意味着相同的IIS机器可以处理更多并发请求,而不是运行得更快。

You should also consider that blocking calls start with a CPU-intensive spinwait. During stress times, blocking calls will result in escalating delays and app pool recycling. Asynchronous calls simply avoid this

您还应该考虑阻塞调用从cpu密集型的spinwait开始。在压力大的时候,阻塞电话会导致延迟升级和应用程序池回收。异步调用可以避免这种情况

#2


178  

You may find my MSDN article on the subject helpful; I took a lot of space in that article describing when you should use async on ASP.NET, not just how to use async on ASP.NET.

你可能会发现我关于这个主题的MSDN文章很有帮助;我在那篇文章中花了很多篇幅来描述什么时候应该在ASP上使用异步。NET,而不仅仅是如何在ASP.NET中使用异步。

I have some concerns using async actions in ASP.NET MVC. When it improves performance of my apps, and when - not.

在ASP中使用异步操作有一些问题。净MVC。当它改善了我的应用程序的性能,当-不是。

First, understand that async/await is all about freeing up threads. On GUI applications, it's mainly about freeing up the GUI thread so the user experience is better. On server applications (including ASP.NET MVC), it's mainly about freeing up the request thread so the server can scale.

首先,要明白异步/等待就是释放线程。在GUI应用程序中,主要是释放GUI线程,以便用户体验更好。在服务器应用程序(包括ASP)上。它主要是释放请求线程,以便服务器能够伸缩。

In particular, it won't:

特别是,它不会:

  • Make your individual requests complete faster. In fact, they will complete (just a teensy bit) slower.
  • 让你的个人请求完成得更快。事实上,它们将会变得更慢(只是一点点)。
  • Return to the caller/browser when you hit an await. await only "yields" to the ASP.NET thread pool, not to the browser.
  • 当您点击等待时返回到调用者/浏览器。只等待ASP的“收益率”。NET线程池,而不是浏览器。

First question is - is it good to use async action everywhere in ASP.NET MVC?

第一个问题是——在ASP中使用异步操作是否合适?净MVC吗?

I'd say it's good to use it everywhere you're doing I/O. It may not necessarily be beneficial, though (see below).

我认为在你做I/O的任何地方使用它都很好。然而,这未必是有益的(见下文)。

However, it's bad to use it for CPU-bound methods. Sometimes devs think they can get the benefits of async by just calling Task.Run in their controllers, and this is a horrible idea. Because that code ends up freeing up the request thread by taking up another thread, so there's no benefit at all (and in fact, they're taking the penalty of extra thread switches)!

但是,对于cpu绑定的方法使用它是不好的。有时devs认为通过调用任务可以获得异步的好处。在控制器中运行,这是一个可怕的想法。因为这段代码最终通过占用另一个线程来释放请求线程,所以根本没有任何好处(事实上,它们要承担额外的线程切换的代价)!

Shall I use async/await keywords when I want to query database (via EF/NHibernate/other ORM)?

当我想查询数据库(通过EF/NHibernate/其他ORM)时,是否要使用异步/等待关键字?

You could use whatever awaitable methods you have available. Right now most of the major players support async, but there are a few that don't. If your ORM doesn't support async, then don't try to wrap it in Task.Run or anything like that (see above).

你可以使用任何你可用的可爱方法。目前大多数主要的参与者都支持异步,但也有少数人不支持异步。如果您的ORM不支持异步,那么不要尝试将其封装到任务中。运行或类似的东西(见上面)。

Note that I said "you could use". If you're talking about ASP.NET MVC with a single database backend, then you're (almost certainly) not going to get any scalability benefit from async. This is because IIS can handle far more concurrent requests than a single instance of SQL server (or other classic RDBMS). However, if your backend is more modern - a SQL server cluster, Azure SQL, NoSQL, etc - and your backend can scale, and your scalability bottleneck is IIS, then you can get a scalability benefit from async.

注意,我说过“您可以使用”。如果你说的是ASP。NET MVC只有一个数据库后端,那么您(几乎肯定)不会从异步中获得任何可伸缩性的好处。这是因为IIS可以处理比单个SQL server(或其他传统RDBMS)更多的并发请求。但是,如果您的后端更现代——SQL server集群、Azure SQL、NoSQL等等——并且您的后端可以伸缩,并且您的可伸缩性瓶颈是IIS,那么您可以从异步中获得可伸缩性好处。

Third question - How many times I can use await keywords to query database asynchronously in ONE single action method?

第三个问题——我可以在一个动作方法中使用多少次等待关键字以异步方式查询数据库?

As many as you like. However, note that many ORMs have a one-operation-per-connection rule. In particular, EF only allows a single operation per DbContext; this is true whether the operation is synchronous or asynchronous.

你想要多少就给多少。但是,请注意,许多orm都有一个每个连接一个操作的规则。特别是,EF只允许每个DbContext执行一个操作;无论操作是同步的还是异步的,都是如此。

Also, keep in mind the scalability of your backend again. If you're hitting a single instance of SQL Server, and your IIS is already capable of keeping SQLServer at full capacity, then doubling or tripling the pressure on SQLServer is not going to help you at all.

此外,请再次记住后端的可伸缩性。如果您正在访问一个SQLServer实例,而您的IIS已经能够使SQLServer处于最大容量,那么SQLServer上的压力增加一倍或三倍对您没有任何帮助。

#3


18  

is it good to use async action everywhere in ASP.NET MVC?

在ASP中到处使用异步操作是否好?净MVC吗?

As usual in programming, it depends. There is always a trade-off when going down a certain path.

在编程中,这要视情况而定。当沿着某条路走下去的时候,总会有一种取舍。

async-await shines in places where you know you'll receiving concurrent requests to your service and you want to be able to scale out well. How does async-await help with scaling out? In the fact that when you invoke a async IO call synchronously, such as a network call or hitting your database, the current thread which is responsible for the execution is blocked waiting for the request to finish. When you use async-await, you enable the framework to create a state machine for you which makes sure that after the IO call is complete, your method continues executing from where it left off.

异步等待在您知道您将接收到对您的服务的并发请求并且希望能够很好地扩展的地方发挥作用。异步等待如何帮助扩展?事实上,当您同步调用异步IO调用(例如网络调用或访问数据库)时,负责执行的当前线程被阻塞,等待请求完成。当您使用异步等待时,您可以让框架为您创建一个状态机,以确保在IO调用完成之后,您的方法继续从它停止的地方执行。

A thing to note is that this state machine has a subtle overhead. Making a method asynchronous does not make it execute faster, and that is an important factor to understand and a misconception many people have.

需要注意的是,这个状态机有一个微妙的开销。使方法异步并不会使其执行速度更快,这是理解和误解的一个重要因素。

Another thing to take under consideration when using async-await is the fact that it is async all the way, meaning that you'll see async penetrate your entire call stack, top to buttom. This means that if you want to expose synchronous API's, you'll often find yourself duplicating a certain amount of code, as async and sync don't mix very well.

使用异步等待时需要考虑的另一件事是,它始终是异步的,这意味着您将看到异步贯穿您的整个调用堆栈,从上到下。这意味着,如果您想公开同步API,您将经常发现自己复制了一定数量的代码,因为异步和同步不能很好地混合在一起。

Shall I use async/await keywords when I want to query database (via EF/NHibernate/other ORM)?

当我想查询数据库(通过EF/NHibernate/其他ORM)时,是否要使用异步/等待关键字?

If you choose to go down the path of using async IO calls, then yes, async-await will be a good choice, as more and more modern database providers expose async method implementing the TAP (Task Asynchronous Pattern).

如果您选择使用异步IO调用,那么是的,异步等待将是一个很好的选择,因为越来越多的现代数据库提供者公开实现TAP(任务异步模式)的异步方法。

How many times I can use await keywords to query database asynchronously in ONE single action method?

我可以使用等待关键字在一个操作方法中异步查询数据库多少次?

As many as you want, as long as you follow the rules stated by your database provider. There is no limit to the amount of async calls you can make. If you have queries which are independent of each other and can be made concurrently, you can spin a new task for each and use await Task.WhenAll to wait for both to complete.

只要您遵循数据库提供者声明的规则,您想要多少就可以多少。您可以进行异步调用的数量没有限制。如果您有相互独立的查询,并且可以同时进行查询,您可以为每个查询旋转一个新任务并使用wait task。什么时候都要等到两者都完成。

#4


6  

async actions help best when the actions does some I\O operations to DB or some network bound calls where the thread that processes the request will be stalled before it gets answer from the DB or network bound call which you just invoked. It's best you use await with them and it will really improve the responsiveness of your application (because less ASP input\output threads will be stalled while waiting for the DB or any other operation like that). In all my applications whenever many calls to DB very necessary I've always wrapped them in awaiatable method and called that with await keyword.

当这些操作对DB或某些网络绑定调用执行一些I\O操作时,异步操作将发挥最大的帮助作用。在这些调用中,处理请求的线程在从刚刚调用的DB或网络绑定调用中得到应答之前将会被阻塞。最好使用wait,它将真正提高应用程序的响应能力(因为等待DB或类似的任何操作时,将会暂停较少的ASP输入\输出线程)。在我所有的应用程序中,每当需要多次调用DB时,我总是将它们封装在可爱的方法中,并使用wait关键字调用它。

#5


6  

My 5 cents:

我的5美分。

  1. Use async/await if and only if you do an IO operation, like DB or external service webservice.
  2. 如果并仅当您执行IO操作(如DB或外部服务webservice)时,请使用async/ wait。
  3. Always prefer async calls to DB.
  4. 总是倾向于异步调用DB。
  5. Each time you query the DB.
  6. 每次查询DB时。

P.S. There are exceptional cases for point 1, but you need to have a good understanding of async internals for this.

对于第一点,有一些例外的情况,但是您需要对异步的内部原理有很好的理解。

As an additional advantage, you can do few IO calls in parallel if needed:

作为一个额外的优势,如果需要,您可以并行地执行很少的IO调用:

Task task1 = FooAsync(); // launch it, but don't wait for result
Task task2 = BarAsync(); // launch bar; now both foo and bar are running
await Task.WhenAll(task1, task2); // this is better in regard to exception handling
// use task1.Result, task2.Result

#6


2  

As you know, MVC supports asynchronous controllers and you should take advantage of it. In case your Controller, performs a lengthy operation, (it might be a disk based I/o or a network call to another remote service), if the request is handled in synchronous manner, the IIS thread is busy the whole time. As a result, the thread is just waiting for the lengthy operation to complete. It can be better utilized by serving other requests while the operation requested in first is under progress. This will help in serving more concurrent requests. Your webservice will be highly scalable and will not easily run into C10k problem. It is a good idea to use async/await for db queries. and yes you can use them as many number of times as you deem fit.

正如您所知道的,MVC支持异步控制器,您应该利用它。如果您的控制器执行冗长的操作(可能是基于磁盘的I/o或对另一个远程服务的网络调用),如果以同步方式处理请求,则IIS线程一直在忙。因此,线程只需要等待冗长的操作完成。当first中请求的操作正在进行时,通过服务其他请求可以更好地利用它。这将有助于提供更多并发请求。您的webservice将具有高度的可伸缩性,并且不会轻易地遇到C10k问题。使用异步/等待db查询是一个好主意。是的,你可以在你认为合适的时间使用它们。

Take a look here for excellent advise.

看看这里有什么好建议。

#1


85  

Asynchronous action methods are useful when an action must perform several independent long running operations.

当一个操作必须执行几个独立的长运行操作时,异步操作方法是有用的。

A typical use for the AsyncController class is long-running Web service calls.

AsyncController类的一个典型用法是长时间运行的Web服务调用。

Should my database calls be asynchronous ?

我的数据库调用应该是异步的吗?

The IIS thread pool can often handle many more simultaneous blocking requests than a database server. If the database is the bottleneck, asynchronous calls will not speed up the database response. Without a throttling mechanism, efficiently dispatching more work to an overwhelmed database server by using asynchronous calls merely shifts more of the burden to the database. If your DB is the bottleneck, asynchronous calls won’t be the magic bullet.

IIS线程池通常可以处理比数据库服务器更多的同时阻塞请求。如果数据库是瓶颈,异步调用不会加快数据库响应。如果没有节流机制,通过使用异步调用有效地将更多的工作分派给一个不堪重负的数据库服务器,只会将更多的负担转移到数据库中。如果您的DB是瓶颈,异步调用就不是灵丹妙药。

You should have a look at 1 and 2 references

你应该看看1和2个参考文献

Derived from @PanagiotisKanavos comments:

来自@PanagiotisKanavos评论:

Moreover, async doesn't mean parallel. Asynchronous execution frees a valuable threadpool thread from blocking for an external resource, for no complexity or performance cost. This means the same IIS machine can handle more concurrent requests, not that it will run faster.

而且,异步并不意味着并行。异步执行使有价值的threadpool线程不必为外部资源阻塞,而无需付出任何复杂性或性能成本。这意味着相同的IIS机器可以处理更多并发请求,而不是运行得更快。

You should also consider that blocking calls start with a CPU-intensive spinwait. During stress times, blocking calls will result in escalating delays and app pool recycling. Asynchronous calls simply avoid this

您还应该考虑阻塞调用从cpu密集型的spinwait开始。在压力大的时候,阻塞电话会导致延迟升级和应用程序池回收。异步调用可以避免这种情况

#2


178  

You may find my MSDN article on the subject helpful; I took a lot of space in that article describing when you should use async on ASP.NET, not just how to use async on ASP.NET.

你可能会发现我关于这个主题的MSDN文章很有帮助;我在那篇文章中花了很多篇幅来描述什么时候应该在ASP上使用异步。NET,而不仅仅是如何在ASP.NET中使用异步。

I have some concerns using async actions in ASP.NET MVC. When it improves performance of my apps, and when - not.

在ASP中使用异步操作有一些问题。净MVC。当它改善了我的应用程序的性能,当-不是。

First, understand that async/await is all about freeing up threads. On GUI applications, it's mainly about freeing up the GUI thread so the user experience is better. On server applications (including ASP.NET MVC), it's mainly about freeing up the request thread so the server can scale.

首先,要明白异步/等待就是释放线程。在GUI应用程序中,主要是释放GUI线程,以便用户体验更好。在服务器应用程序(包括ASP)上。它主要是释放请求线程,以便服务器能够伸缩。

In particular, it won't:

特别是,它不会:

  • Make your individual requests complete faster. In fact, they will complete (just a teensy bit) slower.
  • 让你的个人请求完成得更快。事实上,它们将会变得更慢(只是一点点)。
  • Return to the caller/browser when you hit an await. await only "yields" to the ASP.NET thread pool, not to the browser.
  • 当您点击等待时返回到调用者/浏览器。只等待ASP的“收益率”。NET线程池,而不是浏览器。

First question is - is it good to use async action everywhere in ASP.NET MVC?

第一个问题是——在ASP中使用异步操作是否合适?净MVC吗?

I'd say it's good to use it everywhere you're doing I/O. It may not necessarily be beneficial, though (see below).

我认为在你做I/O的任何地方使用它都很好。然而,这未必是有益的(见下文)。

However, it's bad to use it for CPU-bound methods. Sometimes devs think they can get the benefits of async by just calling Task.Run in their controllers, and this is a horrible idea. Because that code ends up freeing up the request thread by taking up another thread, so there's no benefit at all (and in fact, they're taking the penalty of extra thread switches)!

但是,对于cpu绑定的方法使用它是不好的。有时devs认为通过调用任务可以获得异步的好处。在控制器中运行,这是一个可怕的想法。因为这段代码最终通过占用另一个线程来释放请求线程,所以根本没有任何好处(事实上,它们要承担额外的线程切换的代价)!

Shall I use async/await keywords when I want to query database (via EF/NHibernate/other ORM)?

当我想查询数据库(通过EF/NHibernate/其他ORM)时,是否要使用异步/等待关键字?

You could use whatever awaitable methods you have available. Right now most of the major players support async, but there are a few that don't. If your ORM doesn't support async, then don't try to wrap it in Task.Run or anything like that (see above).

你可以使用任何你可用的可爱方法。目前大多数主要的参与者都支持异步,但也有少数人不支持异步。如果您的ORM不支持异步,那么不要尝试将其封装到任务中。运行或类似的东西(见上面)。

Note that I said "you could use". If you're talking about ASP.NET MVC with a single database backend, then you're (almost certainly) not going to get any scalability benefit from async. This is because IIS can handle far more concurrent requests than a single instance of SQL server (or other classic RDBMS). However, if your backend is more modern - a SQL server cluster, Azure SQL, NoSQL, etc - and your backend can scale, and your scalability bottleneck is IIS, then you can get a scalability benefit from async.

注意,我说过“您可以使用”。如果你说的是ASP。NET MVC只有一个数据库后端,那么您(几乎肯定)不会从异步中获得任何可伸缩性的好处。这是因为IIS可以处理比单个SQL server(或其他传统RDBMS)更多的并发请求。但是,如果您的后端更现代——SQL server集群、Azure SQL、NoSQL等等——并且您的后端可以伸缩,并且您的可伸缩性瓶颈是IIS,那么您可以从异步中获得可伸缩性好处。

Third question - How many times I can use await keywords to query database asynchronously in ONE single action method?

第三个问题——我可以在一个动作方法中使用多少次等待关键字以异步方式查询数据库?

As many as you like. However, note that many ORMs have a one-operation-per-connection rule. In particular, EF only allows a single operation per DbContext; this is true whether the operation is synchronous or asynchronous.

你想要多少就给多少。但是,请注意,许多orm都有一个每个连接一个操作的规则。特别是,EF只允许每个DbContext执行一个操作;无论操作是同步的还是异步的,都是如此。

Also, keep in mind the scalability of your backend again. If you're hitting a single instance of SQL Server, and your IIS is already capable of keeping SQLServer at full capacity, then doubling or tripling the pressure on SQLServer is not going to help you at all.

此外,请再次记住后端的可伸缩性。如果您正在访问一个SQLServer实例,而您的IIS已经能够使SQLServer处于最大容量,那么SQLServer上的压力增加一倍或三倍对您没有任何帮助。

#3


18  

is it good to use async action everywhere in ASP.NET MVC?

在ASP中到处使用异步操作是否好?净MVC吗?

As usual in programming, it depends. There is always a trade-off when going down a certain path.

在编程中,这要视情况而定。当沿着某条路走下去的时候,总会有一种取舍。

async-await shines in places where you know you'll receiving concurrent requests to your service and you want to be able to scale out well. How does async-await help with scaling out? In the fact that when you invoke a async IO call synchronously, such as a network call or hitting your database, the current thread which is responsible for the execution is blocked waiting for the request to finish. When you use async-await, you enable the framework to create a state machine for you which makes sure that after the IO call is complete, your method continues executing from where it left off.

异步等待在您知道您将接收到对您的服务的并发请求并且希望能够很好地扩展的地方发挥作用。异步等待如何帮助扩展?事实上,当您同步调用异步IO调用(例如网络调用或访问数据库)时,负责执行的当前线程被阻塞,等待请求完成。当您使用异步等待时,您可以让框架为您创建一个状态机,以确保在IO调用完成之后,您的方法继续从它停止的地方执行。

A thing to note is that this state machine has a subtle overhead. Making a method asynchronous does not make it execute faster, and that is an important factor to understand and a misconception many people have.

需要注意的是,这个状态机有一个微妙的开销。使方法异步并不会使其执行速度更快,这是理解和误解的一个重要因素。

Another thing to take under consideration when using async-await is the fact that it is async all the way, meaning that you'll see async penetrate your entire call stack, top to buttom. This means that if you want to expose synchronous API's, you'll often find yourself duplicating a certain amount of code, as async and sync don't mix very well.

使用异步等待时需要考虑的另一件事是,它始终是异步的,这意味着您将看到异步贯穿您的整个调用堆栈,从上到下。这意味着,如果您想公开同步API,您将经常发现自己复制了一定数量的代码,因为异步和同步不能很好地混合在一起。

Shall I use async/await keywords when I want to query database (via EF/NHibernate/other ORM)?

当我想查询数据库(通过EF/NHibernate/其他ORM)时,是否要使用异步/等待关键字?

If you choose to go down the path of using async IO calls, then yes, async-await will be a good choice, as more and more modern database providers expose async method implementing the TAP (Task Asynchronous Pattern).

如果您选择使用异步IO调用,那么是的,异步等待将是一个很好的选择,因为越来越多的现代数据库提供者公开实现TAP(任务异步模式)的异步方法。

How many times I can use await keywords to query database asynchronously in ONE single action method?

我可以使用等待关键字在一个操作方法中异步查询数据库多少次?

As many as you want, as long as you follow the rules stated by your database provider. There is no limit to the amount of async calls you can make. If you have queries which are independent of each other and can be made concurrently, you can spin a new task for each and use await Task.WhenAll to wait for both to complete.

只要您遵循数据库提供者声明的规则,您想要多少就可以多少。您可以进行异步调用的数量没有限制。如果您有相互独立的查询,并且可以同时进行查询,您可以为每个查询旋转一个新任务并使用wait task。什么时候都要等到两者都完成。

#4


6  

async actions help best when the actions does some I\O operations to DB or some network bound calls where the thread that processes the request will be stalled before it gets answer from the DB or network bound call which you just invoked. It's best you use await with them and it will really improve the responsiveness of your application (because less ASP input\output threads will be stalled while waiting for the DB or any other operation like that). In all my applications whenever many calls to DB very necessary I've always wrapped them in awaiatable method and called that with await keyword.

当这些操作对DB或某些网络绑定调用执行一些I\O操作时,异步操作将发挥最大的帮助作用。在这些调用中,处理请求的线程在从刚刚调用的DB或网络绑定调用中得到应答之前将会被阻塞。最好使用wait,它将真正提高应用程序的响应能力(因为等待DB或类似的任何操作时,将会暂停较少的ASP输入\输出线程)。在我所有的应用程序中,每当需要多次调用DB时,我总是将它们封装在可爱的方法中,并使用wait关键字调用它。

#5


6  

My 5 cents:

我的5美分。

  1. Use async/await if and only if you do an IO operation, like DB or external service webservice.
  2. 如果并仅当您执行IO操作(如DB或外部服务webservice)时,请使用async/ wait。
  3. Always prefer async calls to DB.
  4. 总是倾向于异步调用DB。
  5. Each time you query the DB.
  6. 每次查询DB时。

P.S. There are exceptional cases for point 1, but you need to have a good understanding of async internals for this.

对于第一点,有一些例外的情况,但是您需要对异步的内部原理有很好的理解。

As an additional advantage, you can do few IO calls in parallel if needed:

作为一个额外的优势,如果需要,您可以并行地执行很少的IO调用:

Task task1 = FooAsync(); // launch it, but don't wait for result
Task task2 = BarAsync(); // launch bar; now both foo and bar are running
await Task.WhenAll(task1, task2); // this is better in regard to exception handling
// use task1.Result, task2.Result

#6


2  

As you know, MVC supports asynchronous controllers and you should take advantage of it. In case your Controller, performs a lengthy operation, (it might be a disk based I/o or a network call to another remote service), if the request is handled in synchronous manner, the IIS thread is busy the whole time. As a result, the thread is just waiting for the lengthy operation to complete. It can be better utilized by serving other requests while the operation requested in first is under progress. This will help in serving more concurrent requests. Your webservice will be highly scalable and will not easily run into C10k problem. It is a good idea to use async/await for db queries. and yes you can use them as many number of times as you deem fit.

正如您所知道的,MVC支持异步控制器,您应该利用它。如果您的控制器执行冗长的操作(可能是基于磁盘的I/o或对另一个远程服务的网络调用),如果以同步方式处理请求,则IIS线程一直在忙。因此,线程只需要等待冗长的操作完成。当first中请求的操作正在进行时,通过服务其他请求可以更好地利用它。这将有助于提供更多并发请求。您的webservice将具有高度的可伸缩性,并且不会轻易地遇到C10k问题。使用异步/等待db查询是一个好主意。是的,你可以在你认为合适的时间使用它们。

Take a look here for excellent advise.

看看这里有什么好建议。