ASP.NET的WebAPI 2异步过滤器 [英] ASP.NET WebAPI 2 Async filters

查看:220
本文介绍了ASP.NET的WebAPI 2异步过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2的WebAPI支持异步过滤方法。
我没有看到这个异步过滤器方法的意义也许我missunderstand呢?
由于过滤器所需要的控制器的方法之前执行它具有反正同步运行!?为什么要异步过滤带来好处呢?有它的东西做的线程从处理的WebAPI?
是我的问题清楚了吗?
预先感谢您的回答!
最好
劳林

WebAPI 2 supports async filter methods. 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? Is my question clear? Thank you in advance for your answer! best laurin

推荐答案

异步和并行是两回事。你不能并行运行的操作方法和行动过滤器,因为你将要过滤( OnActionExecuting )首先执行。然而, OnActionExecuting 是异步的,只是意味着它是非阻塞的。

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. However, OnActionExecuting being asynchronous just means it is non-blocking.

比方说,你必须做出长期运行的网络电话,可能是10秒从你的过滤器code内运行一个HTTP请求。在同步(或阻塞)滤波器code,运行过滤器code将干脆被封锁了10秒钟,直到HTTP调用返回线程的情况下。在这10秒时,线程被阻塞简单,并没有做什么有用的东西。

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. 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. During these 10 seconds, the thread was simply blocked and was not doing anything useful.

在异步滤波器的情况下,运行过滤器的螺纹不会被阻塞。相反,它会返回池中并准备而10秒长的HTTP调用正在进行以服务其他请求。而该线程返回到池中,网络API流水线执行被暂停。一旦HTTP调用完成,结果是可用的,过滤器code继续在同一个线程执行或其他线程和Web API管道开始从它被暂停的点运行。

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. While the thread is returned to the pool, the web API pipeline execution is paused. 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.

所以,仅仅因为过滤code是异步的,这并不意味着流水线执行时,HTTP调用发射了由过滤过程中,将继续前进。仅将其运行管道线程没有被阻塞并且被释放到池中。

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. 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.

这篇关于ASP.NET的WebAPI 2异步过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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