From 7393f264b090c80f4ef3bca59014917bbd62051f Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 12 Apr 2019 03:06:21 +0100 Subject: [PATCH] Lazy create ResourceInvoker.InvokeResultFilters statemachine --- .../src/Infrastructure/ResourceInvoker.cs | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs b/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs index f61f9ff446..fa64ccc0ff 100644 --- a/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs +++ b/src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs @@ -1049,16 +1049,41 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure } } - private async Task InvokeResultFilters() + private Task InvokeResultFilters() { - var next = State.ResultBegin; - var scope = Scope.Invoker; - var state = (object)null; - var isCompleted = false; - - while (!isCompleted) + try { - await ResultNext(ref next, ref scope, ref state, ref isCompleted); + var next = State.ResultBegin; + var scope = Scope.Invoker; + var state = (object)null; + var isCompleted = false; + + while (!isCompleted) + { + var lastTask = ResultNext(ref next, ref scope, ref state, ref isCompleted); + if (!lastTask.IsCompletedSuccessfully) + { + return Awaited(this, lastTask, next, scope, state, isCompleted); + } + } + + return Task.CompletedTask; + } + catch (Exception ex) + { + // Wrap non task-wrapped exceptions in a Task, + // as this isn't done automatically since the method is not async. + return Task.FromException(ex); + } + + static async Task Awaited(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) + { + await lastTask; + + while (!isCompleted) + { + await invoker.ResultNext(ref next, ref scope, ref state, ref isCompleted); + } } }