@@ -843,14 +843,25 @@ public async ValueTask DisposeAsync()
843
843
{
844
844
}
845
845
846
- switch ( _serviceScope )
846
+ await DisposeAsync ( _serviceScope ) ;
847
+ }
848
+
849
+ private static async ValueTask DisposeAsync ( object obj )
850
+ {
851
+ try
847
852
{
848
- case IAsyncDisposable asyncDisposable :
853
+ if ( obj is IAsyncDisposable asyncDisposable )
854
+ {
849
855
await asyncDisposable . DisposeAsync ( ) ;
850
- break ;
851
- case IDisposable disposable :
856
+ }
857
+ else if ( obj is IDisposable disposable )
858
+ {
852
859
disposable . Dispose ( ) ;
853
- break ;
860
+ }
861
+ }
862
+ catch
863
+ {
864
+ // Ignore.
854
865
}
855
866
}
856
867
@@ -1202,7 +1213,7 @@ async Task ProcessOperationsAsync()
1202
1213
}
1203
1214
finally
1204
1215
{
1205
- ( op as IDisposable ) ? . Dispose ( ) ;
1216
+ await DisposeAsync ( op ) ;
1206
1217
}
1207
1218
}
1208
1219
}
@@ -1255,10 +1266,6 @@ private void RehydrateInternal(IRehydrationContext context)
1255
1266
{
1256
1267
_shared . Logger . LogError ( exception , "Error while rehydrating activation" ) ;
1257
1268
}
1258
- finally
1259
- {
1260
- ( context as IDisposable ) ? . Dispose ( ) ;
1261
- }
1262
1269
}
1263
1270
1264
1271
private void OnDehydrate ( IDehydrationContext context )
@@ -2023,7 +2030,7 @@ private sealed class DeactivationInfo
2023
2030
}
2024
2031
}
2025
2032
2026
- private abstract class Command ( CancellationTokenSource cts )
2033
+ private abstract class Command ( CancellationTokenSource cts ) : IDisposable
2027
2034
{
2028
2035
private bool _disposed ;
2029
2036
private readonly CancellationTokenSource _cts = cts ;
@@ -2040,26 +2047,41 @@ public virtual void Cancel()
2040
2047
2041
2048
public virtual void Dispose ( )
2042
2049
{
2043
- lock ( this )
2050
+ try
2044
2051
{
2045
- _disposed = true ;
2046
- _cts . Dispose ( ) ;
2052
+ lock ( this )
2053
+ {
2054
+ _disposed = true ;
2055
+ _cts . Dispose ( ) ;
2056
+ }
2047
2057
}
2058
+ catch
2059
+ {
2060
+ // Ignore.
2061
+ }
2062
+
2063
+ GC . SuppressFinalize ( this ) ;
2048
2064
}
2049
2065
2050
2066
public sealed class Deactivate ( CancellationTokenSource cts , ActivationState previousState ) : Command ( cts )
2051
2067
{
2052
2068
public ActivationState PreviousState { get ; } = previousState ;
2053
2069
}
2054
2070
2055
- public sealed class Activate ( Dictionary < string , object > ? requestContext , CancellationTokenSource cts ) : Command ( cts ) , IDisposable
2071
+ public sealed class Activate ( Dictionary < string , object > ? requestContext , CancellationTokenSource cts ) : Command ( cts )
2056
2072
{
2057
2073
public Dictionary < string , object > ? RequestContext { get ; } = requestContext ;
2058
2074
}
2059
2075
2060
2076
public sealed class Rehydrate ( IRehydrationContext context ) : Command ( new ( ) )
2061
2077
{
2062
2078
public readonly IRehydrationContext Context = context ;
2079
+
2080
+ public override void Dispose ( )
2081
+ {
2082
+ base . Dispose ( ) ;
2083
+ ( Context as IDisposable ) ? . Dispose ( ) ;
2084
+ }
2063
2085
}
2064
2086
2065
2087
public sealed class Delay ( TimeSpan duration ) : Command ( new ( ) )
0 commit comments