简体   繁体   English

ASP.NET WebAPI 2异步过滤器

[英]ASP.NET WebAPI 2 Async filters

WebAPI 2 supports async filter methods. WebAPI 2支持异步过滤方法。 I don't see the sense of this async filter methods or maybe i missunderstand them? 我没有看到这种异步过滤方法的意义,或者我想念它们? Since the filter needs to be executed before the controller method it has to run synchronously anyway!? 由于过滤器需要在控制器方法之前执行,因此它必须同步运行!? Why should async filter bring an advantage then? 为什么异步过滤器会带来优势呢? Has it something to do with the thread handling from the webapi? 它与webapi中的线程处理有关吗? Is my question clear? 我的问题清楚了吗? Thank you in advance for your answer! 提前谢谢你的回答! best laurin 最好的劳林

Asynchronous and parallel are two different things. 异步和并行是两回事。 You cannot run an action method and action filter in parallel, since you will want the filter ( OnActionExecuting ) to execute first. 您无法并行运行操作方法和操作筛选器,因为您需要首先执行筛选器( OnActionExecuting )。 However, OnActionExecuting being asynchronous just means it is non-blocking. 但是, OnActionExecuting是异步的只是意味着它是非阻塞的。

Let's say you have to make a long-running network call, may be an HTTP request that runs for 10 seconds from within your filter code. 假设您必须进行长时间运行的网络调用,可能是一个HTTP请求,在您的过滤器代码中运行10秒钟。 In case of synchronous (or blocking) filter code, the thread that is running the filter code will simply get blocked for 10 seconds until the HTTP call returns. 在同步(或阻塞)过滤器代码的情况下,运行过滤器代码的线程将被阻塞10秒,直到HTTP调用返回。 During these 10 seconds, the thread was simply blocked and was not doing anything useful. 在这10秒钟内,线程被简单地阻止,并没有做任何有用的事情。

In case of asynchronous filter, the thread running the filter will not be blocked. 在异步过滤器的情况下,不会阻止运行过滤器的线程。 Instead, it will be returned to the pool and will be ready to service some other request while the 10 seconds long HTTP call is in progress. 相反,它将返回到池中,并且在10秒长的HTTP调用正在进行时,它将准备好为其他一些请求提供服务。 While the thread is returned to the pool, the web API pipeline execution is paused. 线程返回池时,Web API管道执行暂停。 Once that HTTP call is complete and the result is available, filter code resumes the execution in the same thread or some other thread and the web API pipeline begins to run from the point where it was paused. 一旦完成HTTP调用并且结果可用,过滤器代码将在同一个线程或其他某个线程中恢复执行,并且Web API管道从其暂停的位置开始运行。

So, just because the filter code is asynchronous, it does not mean the pipeline execution will proceed ahead when the HTTP call fired off by the filter is in progress. 因此,仅仅因为过滤器代码是异步的,并不意味着当过滤器触发的HTTP调用正在进行时,管道执行将继续进行。 Only that the thread which was running the pipeline is not blocked and is released to the pool. 只是运行管道的线程没有被阻塞并被释放到池中。

Say, you are reading a book and you came across something which you are not very clear about and ask your friend who is an expert on the subject. 说,你正在读一本书,你遇到了一些你不太清楚的事情,并问你的朋友谁是这方面的专家。 Your friend is taking a few minutes to answer you. 你的朋友花了几分钟回答你。 During that time, you don't need to continue staring at the book. 在此期间,您无需继续盯着这本书。 At the same time, say you can't keep reading. 同时,说你不能继续阅读。 So, you just place a bookmark and do something very quick such as checking a web site in your phone or something. 因此,您只需放置书签并快速执行某些操作,例如检查手机中的网站或其他内容。 Now, the friend answers, you get what you want and you continue reading. 现在,朋友回答,你得到你想要的东西,然后继续阅读。

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

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