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
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
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.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.
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.
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.