4异步行为过滤器

4异步行为过滤器

本文介绍了在MVC 4异步行为过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作的过滤器,在某些特定的条件下使用时,必须执行web服务调用,以确保当前状态是有效的。这最初似乎像异步/的await的理想人选,但我已经遇到了障碍:

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:

假设一个请求:/测试/ FilteredAction

Assume a request to: /Test/FilteredAction


  • MyCustomActionFilter开始执行

    • 第一个伺机达到声明

    传统我期望的动作过滤器来恢复执行,然后完成控制器动作开始执行之前,但是这不会发生。

    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();
        }
    }
    

    所以我想我的问题是:有没有内置MVC 4异步感知行动过滤器类,或者我应该只是在电话阻断这里

    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?

    推荐答案

    MVC没有一个异步兼容的行为过滤器(但WebAPI确实有一个)。

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

    现在,我推荐你使用 OnActionExecuting 阻塞调用。希望MVC将在未来的美好故事。

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

    更新::您可以的MVC团队添加异步过滤器。

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

    这篇关于在MVC 4异步行为过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:54