异步的WebAPI ActionFilterAttribute。异步模块或处理程序完成,而异步操作仍悬而未决 [英] Async WebApi ActionFilterAttribute. An asynchronous module or handler completed while an asynchronous operation was still pending

查看:2691
本文介绍了异步的WebAPI ActionFilterAttribute。异步模块或处理程序完成,而异步操作仍悬而未决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解的await的等待的一个任务(一个awaitable)来完成。
但我感到困惑的是什么,其实就是

I understand await waits for a task (an awaitable) to complete. But I'm confused about what that actually means.

在code,它的工作

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

在code,它的确实工作:

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

显然错误消息的而异步操作仍有待完成的异步模块或处理程序。告诉我,没有等待异步调用来完成,而是主线程继续。我预计线程继续,但不是当前方法中。我以为线程将返回asp.net栈做一些其他的工作,一旦的await asyncOperation返回()操作完成。

Obviously the error message An asynchronous module or handler completed while an asynchronous operation was still pending. tells me that there was no waiting for the async call to complete but instead the "main" thread continued. I expected the thread to continue but not within the current method. I thought the thread would return to the asp.net stack do some other work and return once the await asyncOperation() operation completed.

我用在其他的地方等着太 - (例如等待Web服务响应) - 我没有碰到类似问题的任何地方。我不知道为什么IActionFilterAttribute行为不同。其实我的Web服务调用可能会采取的方式长于读取响应的内容为一个字符串。

I'm using await in other places too - (e.g. waiting for web service responses) - and I didn't run into similar problems anywhere. I wonder why the IActionFilterAttribute behaves differently. In fact my web service calls probably take way longer than reading the content of the response into a string.

有人可以告诉我吗?我有一种感觉我不理解这个概念。

Can someone please enlighten me? I have the feeling I didn't understand the concept.

推荐答案

添加异步code到返回void是危险的,几乎从来没有你真正想做的事情的方法。见<一href=\"http://stackoverflow.com/questions/8043296/whats-the-difference-between-returning-void-and-returning-a-task\">What's返回void和返回任务之间的区别?。

Adding async code to a method that returns void is dangerous and almost never what you actually want to do. See What's the difference between returning void and returning a Task?.

相反,你需要重写/实施返回任务的方法。在这种情况下,ActionFilterAttribute隐藏了IHttpActionFilter提供,所以你需要实现IActionFilter(ExecuteActionFilterAsync),而不是任务。如果你想用你code作为一个属性,只要确保你也是从属性类派生的。

Instead, you need to override/implement a method that returns a task. In this case, ActionFilterAttribute hides the Task that IHttpActionFilter provides, so you'll need to implement IActionFilter (ExecuteActionFilterAsync) instead. If you want to use you code as an attribute, just make sure you also derive from the Attribute class.

例如:

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

这篇关于异步的WebAPI ActionFilterAttribute。异步模块或处理程序完成,而异步操作仍悬而未决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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