Lazy create ResourceInvoker.InvokeNextResultFilterAwaitedAsync statemachine

This commit is contained in:
Ben Adams 2019-04-12 04:50:09 +01:00 committed by Ryan Nowak
parent 8c97cbe6aa
commit cfc22fe0df
1 changed files with 25 additions and 6 deletions

View File

@ -1366,7 +1366,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
} }
} }
private async Task<ResultExecutedContext> InvokeNextResultFilterAwaitedAsync<TFilter, TFilterAsync>() private Task<ResultExecutedContext> InvokeNextResultFilterAwaitedAsync<TFilter, TFilterAsync>()
where TFilter : class, IResultFilter where TFilter : class, IResultFilter
where TFilterAsync : class, IAsyncResultFilter where TFilterAsync : class, IAsyncResultFilter
{ {
@ -1375,6 +1375,28 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
{ {
// If we get here, it means that an async filter set cancel == true AND called next(). // If we get here, it means that an async filter set cancel == true AND called next().
// This is forbidden. // This is forbidden.
return Throw();
}
var task = InvokeNextResultFilterAsync<TFilter, TFilterAsync>();
if (!task.IsCompletedSuccessfully)
{
return Awaited(this, task);
}
Debug.Assert(_resultExecutedContext != null);
return Task.FromResult(_resultExecutedContext);
static async Task<ResultExecutedContext> Awaited(ResourceInvoker invoker, Task task)
{
await task;
Debug.Assert(invoker._resultExecutedContext != null);
return invoker._resultExecutedContext;
}
#pragma warning disable CS1998
static async Task<ResultExecutedContext> Throw()
{
var message = Resources.FormatAsyncResultFilter_InvalidShortCircuit( var message = Resources.FormatAsyncResultFilter_InvalidShortCircuit(
typeof(IAsyncResultFilter).Name, typeof(IAsyncResultFilter).Name,
nameof(ResultExecutingContext.Cancel), nameof(ResultExecutingContext.Cancel),
@ -1383,13 +1405,10 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
#pragma warning restore CS1998
await InvokeNextResultFilterAsync<TFilter, TFilterAsync>();
Debug.Assert(_resultExecutedContext != null);
return _resultExecutedContext;
} }
private static void Rethrow(ResourceExecutedContext context) private static void Rethrow(ResourceExecutedContext context)
{ {
if (context == null) if (context == null)