多等待异步在.net中的WebAPI [英] Multiple Await Async in .Net WebApi

查看:216
本文介绍了多等待异步在.net中的WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些异步控制器和服务,让使用等待 / 异步关键字。

若干的行动看起来有点像:

 公共异步任务< SomeViewModel>获取(INT ID)
{
    VAR someData =等待_service.GetData(ID);
    VAR someOtherData =等待_service.GetMoreData(ID);
    返回新SomeViewModel
    {
        数据= someData,
        OtherData = someOtherData,
    }
}

这可能是该服务调用本身也有multuple 等待秒。在等待通常是针对异步来电实体框架,一个服务总线或第三方网络端点。

我的一个同事走过来的今天暗示,这种code的是毫无意义的,它只会产生线程管理额外的工作和负载下,我们将真正的产生更多的工作,为运行时和慢下来的应用程序作为一个结果。

他们是正确的,如果是什么被认为是最佳实践异步 / 等待,当你有多个IO绑定在Web API请求调用?


解决方案

  

我的一个同事走过来的今天暗示,这种code的是毫无意义的,它只会产生线程管理额外的工作和负载下,我们将真正的产生更多的工作,为运行时和慢下来的应用程序作为一个结果。


这是有趣的,因为相反的是真的。至于其他的回答者指出,如果你使用真正的异步操作(即不 Task.Run 或类似的东西),然后有较少的 使用的线程和应用程序响应的更好的负载下。

有些人(不是我)所做的平均的ASP.NET应用程序过渡研究,以异步,他们已经发现了移动时10倍提高到100倍的可扩展性增加异步,而不是阻塞调用。你可以期望更好的扩展性,如果你的应用程序有更多的异步工作要做。

如果你看一下的的请求,如果每次操作在同一时间做了一个,然后异步版本的的慢。但是,如果你考虑系统作为一个整体 - 尤其的负载下 - 异步版本能更好地伸缩。常常被忽视异步处理程序的另一个方面是,异步版本的响应速度比线程池突然负载可通过本身

此外,异步code可以很容易地执行并发请求,它可以使个人的要求也更快:

 公共异步任务< SomeViewModel>获取(INT ID)
{
  变种someDataTask = _service.GetData(ID);
  变种someOtherDataTask = _service.GetMoreData(ID);
  等待Task.WhenAll(someDataTask,someOtherDataTask);
  返回新SomeViewModel
  {
    数据=等待someDataTask,
    OtherData =等待someOtherDataTask,
  }
}

We have a number of async controllers and services that make use of the await/async keywords.

A number of the actions look a little like:

public async Task<SomeViewModel> Get(int id)
{
    var someData = await _service.GetData(id);
    var someOtherData = await _service.GetMoreData(id);
    return new SomeViewModel
    {
        Data = someData,
        OtherData = someOtherData,
    }
}

It might be that the service calls themselves also have multuple awaits. The await will typically be against an async call to entity framework, a service bus or a 3rd party web endpoint.

One of my colleagues came to be today suggesting that this kind of code was pointless, that it would simply generate extra work for thread management and the under load we would be actually generate more work for the for the runtime and slow the app down as a result.

Are they right and if so what is considered best practise for async / await when you have multiple IO bound calls in a Web API request?

解决方案

One of my colleagues came to be today suggesting that this kind of code was pointless, that it would simply generate extra work for thread management and the under load we would be actually generate more work for the for the runtime and slow the app down as a result.

That's amusing, since the opposite is actually true. As other answerers have pointed out, if you're using true asynchronous operations (i.e., not Task.Run or anything like that), then there are fewer threads used and the application responds better under load.

Some folks (not me) have done studies on "average" ASP.NET applications transitioning to async, and they have found a 10x to 100x scalability increase when moving to async as opposed to blocking calls. You can expect better scalability if your application has more asynchronous work to do.

If you look at a single request, and if each operation is done one at a time, then the asynchronous version is slightly slower. But if you consider the system as a whole - especially under load - the asynchronous version scales better. Another aspect of asynchronous handlers that is often overlooked is that the asynchronous version responds faster to sudden loads than the thread pool can by itself.

Also, asynchronous code makes it easy to execute concurrent requests, which can make the individual requests faster as well:

public async Task<SomeViewModel> Get(int id)
{
  var someDataTask = _service.GetData(id);
  var someOtherDataTask = _service.GetMoreData(id);
  await Task.WhenAll(someDataTask, someOtherDataTask);
  return new SomeViewModel
  {
    Data = await someDataTask,
    OtherData = await someOtherDataTask,
  }
}

这篇关于多等待异步在.net中的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆