在 ActionFilterAttribute 上的 OnActionExecutedAsync 中等待
await within OnActionExecutedAsync on ActionFilterAttribute
我无法覆盖 WebApi 项目中的 OnActionExecutedAsync
method of the ActionFilterAttribute
。到目前为止,我有以下 C# 代码
public class MyFilter : ActionFilterAttribute
{
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionContext, CancellationToken cancellationToken)
{
var response = await MyAsyncMethod();
if(response.SomeProperty)
DoThing();
}
}
但是在调试代码时,对 await 方法的响应从不 returns。对于任何异步方法都是如此,因为我已经测试了一些我知道可以在我的代码库的其他地方使用的方法。我也尝试过 void 方法,以及使用 .Wait()
和 .Result
都有同样的问题。
var response = MyAsyncMethod().Result;
await MyVoidAsyncMethod();
MyVoidAsyncMethod().Wait(cancellationToken);
所以我认为问题在于等待 OnActionExecutedAsync 方法中的任何方法。
我注意到我可以毫无问题地等待基本方法。
await base.OnActionExecutedAsync(actionContext, cancellationToken);
如何在 OnActionExecutedAsync
方法中调用异步方法?
更新示例显示纯粹等待的方法
由于建议通过确保等待链中的所有方法来解决评论中的问题,我添加了一个示例,该示例仅显示仍然导致问题的等待方法。
public class MyFilter : ActionFilterAttribute
{
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionContext, CancellationToken cancellationToken)
{
await base.OnActionExecutedAsync(actionContext, cancellationToken);
await DefinitelyAllAsync();
}
private async Task DefinitelyAllAsync()
{
var request = WebRequest.Create("http://www.whosebug.com");
var response = await Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null) as HttpWebResponse;
Debug.Assert(response?.StatusCode == HttpStatusCode.OK);
}
}
这永远不会到达 Debug.Assert
。
问题是另一个过滤器在较早的时候对请求起作用,没有正确处理异步。在下面的示例中,AuthFilter
将在其生命周期中比 ActionFilter
更早地处理请求,如果它没有正确实现异步,则线程在遇到其他过滤器时会出现异步问题在请求生命周期的后期。我的错误是假设 ActionFilter
在它自己的线程上是 运行,因为它是我的代码的入口点。以下代码显示了两个过滤器都正确地实现了 await
运算符。
Global.asax.cs
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Filters.Add(new AuthFilter());
GlobalConfiguration.Configuration.Filters.Add(new ActionFilter());
}
AuthFilter.cs
public class AuthFilter : IAuthorizationFilter
{
public bool AllowMultiple => false;
public async Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
// incorrect use of async in this Filter will break async in future filters
return await continuation();
}
}
ActionFilter.cs
public class ActionFilter : ActionFilterAttribute
{
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionContext, CancellationToken cancellationToken)
{
await base.OnActionExecutedAsync(actionContext, cancellationToken);
await GetWhosebug();
}
private async Task GetWhosebug()
{
var request = WebRequest.Create("http://www.whosebug.com");
var response = await Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null) as HttpWebResponse;
// this debug is unreachable if a past filter has implemented async incorrectly
Debug.Assert(response?.StatusCode == HttpStatusCode.OK);
}
}
我无法覆盖 WebApi 项目中的 OnActionExecutedAsync
method of the ActionFilterAttribute
。到目前为止,我有以下 C# 代码
public class MyFilter : ActionFilterAttribute
{
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionContext, CancellationToken cancellationToken)
{
var response = await MyAsyncMethod();
if(response.SomeProperty)
DoThing();
}
}
但是在调试代码时,对 await 方法的响应从不 returns。对于任何异步方法都是如此,因为我已经测试了一些我知道可以在我的代码库的其他地方使用的方法。我也尝试过 void 方法,以及使用 .Wait()
和 .Result
都有同样的问题。
var response = MyAsyncMethod().Result;
await MyVoidAsyncMethod();
MyVoidAsyncMethod().Wait(cancellationToken);
所以我认为问题在于等待 OnActionExecutedAsync 方法中的任何方法。
我注意到我可以毫无问题地等待基本方法。
await base.OnActionExecutedAsync(actionContext, cancellationToken);
如何在 OnActionExecutedAsync
方法中调用异步方法?
更新示例显示纯粹等待的方法
由于建议通过确保等待链中的所有方法来解决评论中的问题,我添加了一个示例,该示例仅显示仍然导致问题的等待方法。
public class MyFilter : ActionFilterAttribute
{
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionContext, CancellationToken cancellationToken)
{
await base.OnActionExecutedAsync(actionContext, cancellationToken);
await DefinitelyAllAsync();
}
private async Task DefinitelyAllAsync()
{
var request = WebRequest.Create("http://www.whosebug.com");
var response = await Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null) as HttpWebResponse;
Debug.Assert(response?.StatusCode == HttpStatusCode.OK);
}
}
这永远不会到达 Debug.Assert
。
问题是另一个过滤器在较早的时候对请求起作用,没有正确处理异步。在下面的示例中,AuthFilter
将在其生命周期中比 ActionFilter
更早地处理请求,如果它没有正确实现异步,则线程在遇到其他过滤器时会出现异步问题在请求生命周期的后期。我的错误是假设 ActionFilter
在它自己的线程上是 运行,因为它是我的代码的入口点。以下代码显示了两个过滤器都正确地实现了 await
运算符。
Global.asax.cs
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Filters.Add(new AuthFilter());
GlobalConfiguration.Configuration.Filters.Add(new ActionFilter());
}
AuthFilter.cs
public class AuthFilter : IAuthorizationFilter
{
public bool AllowMultiple => false;
public async Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
// incorrect use of async in this Filter will break async in future filters
return await continuation();
}
}
ActionFilter.cs
public class ActionFilter : ActionFilterAttribute
{
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionContext, CancellationToken cancellationToken)
{
await base.OnActionExecutedAsync(actionContext, cancellationToken);
await GetWhosebug();
}
private async Task GetWhosebug()
{
var request = WebRequest.Create("http://www.whosebug.com");
var response = await Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null) as HttpWebResponse;
// this debug is unreachable if a past filter has implemented async incorrectly
Debug.Assert(response?.StatusCode == HttpStatusCode.OK);
}
}