@@ -84,41 +84,22 @@ public virtual Task InvokeAsync()
84
84
}
85
85
86
86
Exception ? releaseException = null ;
87
+ ValueTask releaseResult = default ;
87
88
try
88
89
{
89
- ReleaseResources ( ) ;
90
+ releaseResult = ReleaseResources ( ) ;
90
91
}
91
92
catch ( Exception exception )
92
93
{
93
94
releaseException = exception ;
94
95
}
95
96
96
- Exception ? scopeException = null ;
97
- try
98
- {
99
- scope ? . Dispose ( ) ;
100
- }
101
- catch ( Exception exception )
97
+ if ( releaseException == null && ! releaseResult . IsCompletedSuccessfully )
102
98
{
103
- scopeException = exception ;
99
+ return HandleAsyncReleaseErrors ( releaseResult , scope ) ;
104
100
}
105
101
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 ) ;
122
103
123
104
static async Task Awaited ( ResourceInvoker invoker , Task task , IDisposable ? scope )
124
105
{
@@ -185,6 +166,51 @@ static async Task Logged(ResourceInvoker invoker)
185
166
actionContext . RouteData ) ;
186
167
}
187
168
}
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
+ }
188
214
}
189
215
190
216
/// <summary>
0 commit comments