异步的WebAPI ActionFilterAttribute。异步模块或处理程序完成,而异步操作仍悬而未决

编程入门 行业动态 更新时间:2024-10-10 23:23:27
本文介绍了异步的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栈做一些其他的工作,一旦的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 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是危险的,几乎从来没有你真正想做的事情的方法。见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。异步模块或处理程序完成,而异步操作仍悬而未决

本文发布于:2023-11-26 02:06:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:悬而未决   模块   操作   程序   WebAPI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!