【问题标题】:Async WebApi ActionFilterAttribute. An asynchronous module or handler completed while an asynchronous operation was still pending异步 WebApi ActionFilterAttribute。异步模块或处理程序已完成,而异步操作仍处于挂起状态
【发布时间】:2013-03-08 08:00:21
【问题描述】:

我了解 await 等待 任务(可等待)完成。 但我对这实际上意味着什么感到困惑。

不起作用的代码:

public async override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
    if (actionExecutedContext.Response.Content != null)
    {
        var responseContent = await actionExecutedContext.Response.Content.ReadAsStringAsync();
        DoSomething(responseContent);
    }
}

起作用的代码:

public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
    if (actionExecutedContext.Response.Content != null)
    {
        var responseContent = actionExecutedContext.Response.Content.ReadAsStringAsync().ContinueWith(
        task =>
        {
            DoSomething(task.Result);
        });
    }
}

显然错误消息 异步模块或处理程序已完成,而异步操作仍处于挂起状态。 告诉我没有等待异步调用完成,而是“主”线程继续。我希望线程继续但不在当前方法中。我以为线程会返回到 asp.net 堆栈做一些其他工作,并在等待 asyncOperation() 操作完成后返回。

我也在其他地方使用 await - (例如等待 Web 服务响应) - 我没有在任何地方遇到类似的问题。我想知道为什么 IActionFilterAttribute 行为不同。事实上,我的 Web 服务调用可能比将响应内容读入字符串花费的时间更长。

有人可以请教我吗?我感觉我没有理解这个概念。

【问题讨论】:

  • 动作过滤器(MVC 的一部分,不是 WebAPI)不支持异步操作。如果您需要异步操作过滤器,请尝试使用消息处理程序。哦,还有vote here
  • 这是一个 WebAPI 问题,我正在使用正确的 ActionFilterAttribute (System.Web.Http...) - 你是说它应该有效吗? :)
  • 我明白了。在这种情况下,您可能需要定义自己的AsyncActionFilterAttribute 并实现IActionFilter.ExecuteActionFilterAsync

标签: asp.net-web-api async-await actionfilterattribute


【解决方案1】:

将异步代码添加到返回 void 的方法是危险的,而且几乎绝不是您真正想要做的事情。见What's the difference between returning void and returning a Task?

相反,您需要重写/实现一个返回任务的方法。在这种情况下,ActionFilterAttribute 隐藏了 IHttpActionFilter 提供的任务,因此您需要改为实现 IActionFilter (ExecuteActionFilterAsync)。如果您想将代码用作属性,只需确保您也从 Attribute 类派生。

例如:

public class AsyncActionFilterAttribute : Attribute, IActionFilter
{
    public async Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
        HttpResponseMessage response = await continuation();
        DoSomething(response);
        return response;
    }
}

【讨论】:

    【解决方案2】:

    而不是实施

    public async override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    

    你必须按如下方式实现OnActionExecuted方法的异步版本:

    public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
    

    这样你就可以在方法中使用 await 并且行为会如你所愿。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多