在Web API控制器(.net Core)中使用异步/等待或任务

编程入门 行业动态 更新时间:2024-10-26 06:30:23
本文介绍了在Web API控制器( Core)中使用异步/等待或任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a 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.

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

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)中使用异步/等待或任务

本文发布于:2023-11-16 21:00:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607437.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   API   Web   net   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!