Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c# - Async action filter in MVC 4

I have an action filter that when used in certain specific conditions has to perform a web service call to ensure that the current state is valid. This initially seemed like an ideal candidate for async/await, but I have encountered a snag:

Assume a request to: /Test/FilteredAction

  • MyCustomActionFilter begins executing
    • The first "await" statement is reached
  • TestController.FilteredAction starts executing
  • MyCustomActionFilter resumes executing

Traditionally I would expect the action filter to resume executing and then complete before the controller action starts executing, but this does not happen.

Now I assume this is because I am using:

public class MyCustomActionFilter : ActionFilterAttribute 
{
    public override **async** void OnActionExecuting(FilterContext context) 
    {
        var foo = await WebServiceCall();
    }
}

So I think my question is: Is there an async-aware action filter class built into MVC 4, or should I just block on the calls in here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

MVC does not have an async-compatible action filter (but WebAPI does have one).

For now, I recommend you use blocking calls in OnActionExecuting. Hopefully MVC will have a better story in the future.

Update: You can vote here for the MVC team to add async filters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...