简体   繁体   English

从浏览器调用异步方法

[英]Calling an async method from the browser

From my understanding when we use await and the awaited task is not yet completed then the execution returns to caller . 根据我的理解, 当我们使用await并且等待的任务尚未完成时,执行将返回给调用者 It works fine in server side(calling the async method from server side method itself). 它在服务器端工作正常(从服务器端方法本身调用异步方法)。 But what happends when I call from UI to an async method. 但是当我从UI调用异步方法时会发生什么。

public class TestController : ApiController
{
        IList<string> lstString = new List<string>();
        public async Task<IList<string>> GetMyCollectionAsync()
        {
            lstString.Add("First");
            string secString = await GetSecondString(); // I am expecting a response back to UI from here.
            lstString.Add(secString);
            lstString.Add("Third");
            return lstString;
        }

        private async Task<string> GetSecondString()
        {
            await Task.Delay(5000);
            return "Second after await";
        }
}

I tested with the above API from browser like 我用浏览器中的上述API测试过

http://localhost:port/Test HTTP://本地主机:端口/测试

, but I got response only after 5 sec in my UI. ,但是我的UI中只有5秒后才收到回复。 Am I thinking it wrongly? 我错误地思考了吗?

Am I thinking it wrongly? 我错误地思考了吗?

Yes. 是。 async-await does not change the nature of the HTTP protocol, which is of type request-response. async-await不会改变HTTP协议的性质,HTTP协议的类型是请求 - 响应。 When using async-await inside an ASP.NET controller, using an async method will not yield a response to the caller, it would only yield the requests thread back to the threadpool. 在ASP.NET控制器中使用async-await ,使用async方法不会产生对调用者的响应,它只会将请求线程返回给线程池。

But if this is true, then using async method having a single await in controller side is not useful. 但如果这是真的,那么在控制器端使用具有单个等待的异步方法是没有用的。 right? 对? because it took the same time of synchronous call 因为它花了相同的同步调用时间

Async shines when you need scalability . 当您需要可伸缩性时,异步会闪耀。 It isn't about "yield this response as fast as possible", it's about being able to handle a large amount of requests without exhausting the thread-pool. 它不是“尽可能快地产生这种响应”,它是关于能够处理大量请求而不会耗尽线程池。 Once the thread starts executing IO work, instead of being blocked, it is returned. 一旦线程开始执行IO工作,而不是被阻止,它将被返回。 Thus, able to serve more requests. 因此,能够提供更多的请求。

Async by itself does not make anything "go faster", which is a conception I see people thinking alot. 异步本身并没有使任何“变得更快”,这是我认为人们在思考很多的概念。 If you're not going to be hitting your web service with many concurrent requests, you're most likely not going to be seeing any benefit from using it. 如果您不打算通过许多并发请求访问您的Web服务,那么您很可能不会看到使用它的任何好处。 As @Scott points out, an async method has a slight overhead as it generates a state machine behind the scenes. 正如@Scott指出的那样,异步方法在幕后生成状态机时会有轻微的开销

async/await allow the thread to go off servicing other requests while there is that idle 5 seconds, but ultimately everything in GetMyCollectionAsync has to complete before a response is sent to the client. async / await允许线程在空闲5秒时为其他请求提供服务,但最终GetMyCollectionAsync中的所有内容都必须在响应发送到客户端之前完成。

So I'd expect your code to take 5 seconds and return all 3 strings in the response. 所以我希望你的代码需要5秒钟,并在响应中返回所有3个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM