使WebAPI动作异步? [英] Make WebAPI actions async?

查看:522
本文介绍了使WebAPI动作异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,关于在WebAPI/MVC控制器AJAX请求上使用async/await是否有益.

I've got a question about whether it's beneficial to use async/await on WebAPI / MVC Controller AJAX requests.

说我有这个与WebAPI后端通信的AngularJS应用,我想获取一些数据.我对WebAPI进行了一些AJAX调用,然后等待结果.

Say I've got this AngularJS app that talks to a WebAPI backend and I want to get some data. I do some AJAX call to WebAPI and wait for the results.

就客户端而言,这当然是异步的.如果我要使这些动作异步,对服务器现在有利吗?

In terms of the client, this is of course Async. Would it now be beneficial for the server if I was to make these actions Async?

编辑

我刚刚找到了这个: http://www.dotnetcurry.com/showarticle.aspx ?ID = 948 -说是

I just found this: http://www.dotnetcurry.com/showarticle.aspx?ID=948 - saying it IS

编辑-尝试澄清 让我尝试更加清楚.如果我将WebAPI中所有对服务层的调用实现为异步(.NET关键字)还是正常",那么服务器上的处理器负载会更少吗?

EDIT - attempt to clarify Let me try to be more clear. Will the processor load on the server be less if I implement all the calls to the service layer from my WebAPI as async (the .NET keyword) vs. 'normal'?

在所有方法都不异步的情况下,IIS是否会产生更多线程,从而例如消耗更多内存?

Will IIS spawn more threads in case all the methods are NOT Async, thereby using up more memory for instance?

推荐答案

如果您的操作方法访问了数据库或文件系统,或者发出了另一个网络请求(即需要等待IO的操作),则使操作异步将有一些好处如果您的服务器负载很重.

If your action method hits a database or filesystem, or makes another network request (i.e. something that requires waiting on IO) then making the action async will have some benefit if your server is under heavy load.

这篇关于使WebAPI动作异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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