@@ -83,7 +83,7 @@ public virtual Task InvokeAsync()
83
83
return Awaited ( this , task , scope ) ;
84
84
}
85
85
86
- return ReleaseResourcesCore ( scope ) ;
86
+ return ReleaseResourcesCore ( scope ) . AsTask ( ) ;
87
87
88
88
static async Task Awaited ( ResourceInvoker invoker , Task task , IDisposable ? scope )
89
89
{
@@ -143,27 +143,26 @@ static async Task Logged(ResourceInvoker invoker)
143
143
}
144
144
}
145
145
146
- internal Task ReleaseResourcesCore ( IDisposable ? scope )
146
+ internal ValueTask ReleaseResourcesCore ( IDisposable ? scope )
147
147
{
148
148
Exception ? releaseException = null ;
149
149
ValueTask releaseResult = default ;
150
150
try
151
151
{
152
152
releaseResult = ReleaseResources ( ) ;
153
+ if ( ! releaseResult . IsCompletedSuccessfully )
154
+ {
155
+ return HandleAsyncReleaseErrors ( releaseResult , scope ) ;
156
+ }
153
157
}
154
158
catch ( Exception exception )
155
159
{
156
160
releaseException = exception ;
157
161
}
158
162
159
- if ( releaseException == null && ! releaseResult . IsCompletedSuccessfully )
160
- {
161
- return HandleAsyncReleaseErrors ( releaseResult , scope ) ;
162
- }
163
-
164
163
return HandleReleaseErrors ( scope , releaseException ) ;
165
164
166
- static async Task HandleAsyncReleaseErrors ( ValueTask releaseResult , IDisposable ? scope )
165
+ static async ValueTask HandleAsyncReleaseErrors ( ValueTask releaseResult , IDisposable ? scope )
167
166
{
168
167
Exception ? releaseException = null ;
169
168
try
@@ -178,7 +177,7 @@ static async Task HandleAsyncReleaseErrors(ValueTask releaseResult, IDisposable?
178
177
await HandleReleaseErrors ( scope , releaseException ) ;
179
178
}
180
179
181
- static Task HandleReleaseErrors ( IDisposable ? scope , Exception ? releaseException )
180
+ static ValueTask HandleReleaseErrors ( IDisposable ? scope , Exception ? releaseException )
182
181
{
183
182
Exception ? scopeException = null ;
184
183
try
@@ -192,19 +191,19 @@ static Task HandleReleaseErrors(IDisposable? scope, Exception? releaseException)
192
191
193
192
if ( releaseException == null && scopeException == null )
194
193
{
195
- return Task . CompletedTask ;
194
+ return default ;
196
195
}
197
196
else if ( releaseException != null && scopeException != null )
198
197
{
199
- return Task . FromException ( new AggregateException ( releaseException , scopeException ) ) ;
198
+ return ValueTask . FromException ( new AggregateException ( releaseException , scopeException ) ) ;
200
199
}
201
200
else if ( releaseException != null )
202
201
{
203
- return Task . FromException ( releaseException ) ;
202
+ return ValueTask . FromException ( releaseException ) ;
204
203
}
205
204
else
206
205
{
207
- return Task . FromException ( scopeException ! ) ;
206
+ return ValueTask . FromException ( scopeException ! ) ;
208
207
}
209
208
}
210
209
}
0 commit comments