Skip to content

Commit 762b70e

Browse files
committed
Cleanup invoker code
1 parent c5d44c5 commit 762b70e

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

src/Mvc/Mvc.Core/src/Infrastructure/ResourceInvoker.cs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,22 @@ public virtual Task InvokeAsync()
8484
}
8585

8686
Exception? releaseException = null;
87+
ValueTask releaseResult = default;
8788
try
8889
{
89-
ReleaseResources();
90+
releaseResult = ReleaseResources();
9091
}
9192
catch (Exception exception)
9293
{
9394
releaseException = exception;
9495
}
9596

96-
Exception? scopeException = null;
97-
try
98-
{
99-
scope?.Dispose();
100-
}
101-
catch (Exception exception)
97+
if (releaseException == null && !releaseResult.IsCompletedSuccessfully)
10298
{
103-
scopeException = exception;
99+
return HandleAsyncReleaseErrors(releaseResult, scope);
104100
}
105101

106-
if (releaseException == null && scopeException == null)
107-
{
108-
return Task.CompletedTask;
109-
}
110-
else if (releaseException != null && scopeException != null)
111-
{
112-
return Task.FromException(new AggregateException(releaseException, scopeException));
113-
}
114-
else if (releaseException != null)
115-
{
116-
return Task.FromException(releaseException);
117-
}
118-
else
119-
{
120-
return Task.FromException(scopeException!);
121-
}
102+
return HandleReleaseErrors(scope, releaseException);
122103

123104
static async Task Awaited(ResourceInvoker invoker, Task task, IDisposable? scope)
124105
{
@@ -185,6 +166,51 @@ static async Task Logged(ResourceInvoker invoker)
185166
actionContext.RouteData);
186167
}
187168
}
169+
170+
static async Task HandleAsyncReleaseErrors(ValueTask releaseResult, IDisposable? scope)
171+
{
172+
Exception? releaseException = null;
173+
try
174+
{
175+
await releaseResult;
176+
}
177+
catch (Exception exception)
178+
{
179+
releaseException = exception;
180+
}
181+
182+
await HandleReleaseErrors(scope, releaseException);
183+
}
184+
185+
static Task HandleReleaseErrors(IDisposable? scope, Exception? releaseException)
186+
{
187+
Exception? scopeException = null;
188+
try
189+
{
190+
scope?.Dispose();
191+
}
192+
catch (Exception exception)
193+
{
194+
scopeException = exception;
195+
}
196+
197+
if (releaseException == null && scopeException == null)
198+
{
199+
return Task.CompletedTask;
200+
}
201+
else if (releaseException != null && scopeException != null)
202+
{
203+
return Task.FromException(new AggregateException(releaseException, scopeException));
204+
}
205+
else if (releaseException != null)
206+
{
207+
return Task.FromException(releaseException);
208+
}
209+
else
210+
{
211+
return Task.FromException(scopeException!);
212+
}
213+
}
188214
}
189215

190216
/// <summary>

0 commit comments

Comments
 (0)