在Web API控制器(.net Core)中使用异步/等待或任务 [英] Using async/await or task in web api controller (.net core)

查看:913
本文介绍了在Web API控制器(.net Core)中使用异步/等待或任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.net核心API,该API具有一个控制器,该控制器可构建要返回的聚合对象. 它创建的对象由来自对服务类的3个方法调用的数据组成.这些都彼此独立,并且可以彼此隔离地运行. 目前,我正在使用任务来改善此控制器的性能.当前版本看起来像这样...

I have a .net core API which has a controller that builds an aggregated object to return. the object it creates is made of data that comes from 3 method calls to a service class. These are all independent of each other and can be run in isolation from each other. Currently I am using tasks to improve the performance of this controller. the current version looks something like this...

[HttpGet]
public IActionResult myControllerAction()
{      
    var data1 = new sometype1();
    var data2 = new sometype2();
    var data3 = new List<sometype3>();

    var t1 = new Task(() => { data1 = service.getdata1(); });
    t1.Start();

    var t2 = new Task(() => { data2 = service.getdata2(); });
    t2.Start();

    var t3 = new Task(() => { data3 = service.getdata2(); });
    t3.Start();

    Task.WaitAll(t1, t2, t3);

    var data = new returnObject
    {
         d1 = data1,
         d2 = data2,
         d2 = data3
    };

    return Ok(data);
}

这很好用,但是我想知道使用任务是否是这里最好的解决方案?使用async/await是一个更好的主意和更可接受的方法吗?

This works well however I am wondering if using tasks is the best solution here? Would using async/await be a better idea and more accepted way?

例如,是否应该将控制器标记为异步,并在每次调用服务方法时进行等待?

For example should the controller be marked as async and an await put on each call to the the service methods?

推荐答案

这很好用,但是我想知道使用任务是否是这里最好的解决方案?使用async/await是一个更好的主意和更可接受的方法吗?

This works well however I am wondering if using tasks is the best solution here? Would using async/await be a better idea and more accepted way?

是的,绝对如此.在ASP.NET上执行并行处理会为每个请求消耗多个线程,这可能会严重影响您的可伸缩性.异步处理对于I/O而言要优越得多.

Yes, absolutely. Doing parallel processing on ASP.NET consumes multiple threads per request, which can severely impact your scalability. Asynchronous processing is far superior for I/O.

要使用async,请首先从最低级别的调用开始,在服务内部.可能是在某个时候进行HTTP调用.更改为使用异步HTTP调用(例如HttpClient).然后让async从那里自然生长.

To use async, first start with your lowest-level call, somewhere inside your service. It's probably doing an HTTP call at some point; change that to use asynchronous HTTP calls (e.g., HttpClient). Then let async grow naturally from there.

最终,您将获得异步getdata1Asyncgetdata2Asyncgetdata3Async方法,这些方法可以同时使用,例如:

Eventually, you'll end up with asynchronous getdata1Async, getdata2Async, and getdata3Async methods, which can be consumed concurrently as such:

[HttpGet]
public async Task<IActionResult> myControllerAction()
{
  var t1 = service.getdata1Async();
  var t2 = service.getdata2Async();
  var t3 = service.getdata3Async();
  await Task.WhenAll(t1, t2, t3);

  var data = new returnObject
  {
    d1 = await t1,
    d2 = await t2,
    d3 = await t3
  };

  return Ok(data);
}

使用这种方法,在进行三个服务调用时,myControllerAction使用线程而不是四个.

With this approach, while the three service calls are in progress, myControllerAction uses zero threads instead of four.

这篇关于在Web API控制器(.net Core)中使用异步/等待或任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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