@@ -843,14 +843,25 @@ public async ValueTask DisposeAsync()
843843 {
844844 }
845845
846- switch ( _serviceScope )
846+ await DisposeAsync ( _serviceScope ) ;
847+ }
848+
849+ private static async ValueTask DisposeAsync ( object obj )
850+ {
851+ try
847852 {
848- case IAsyncDisposable asyncDisposable :
853+ if ( obj is IAsyncDisposable asyncDisposable )
854+ {
849855 await asyncDisposable . DisposeAsync ( ) ;
850- break ;
851- case IDisposable disposable :
856+ }
857+ else if ( obj is IDisposable disposable )
858+ {
852859 disposable . Dispose ( ) ;
853- break ;
860+ }
861+ }
862+ catch
863+ {
864+ // Ignore.
854865 }
855866 }
856867
@@ -1202,7 +1213,7 @@ async Task ProcessOperationsAsync()
12021213 }
12031214 finally
12041215 {
1205- ( op as IDisposable ) ? . Dispose ( ) ;
1216+ await DisposeAsync ( op ) ;
12061217 }
12071218 }
12081219 }
@@ -1255,10 +1266,6 @@ private void RehydrateInternal(IRehydrationContext context)
12551266 {
12561267 _shared . Logger . LogError ( exception , "Error while rehydrating activation" ) ;
12571268 }
1258- finally
1259- {
1260- ( context as IDisposable ) ? . Dispose ( ) ;
1261- }
12621269 }
12631270
12641271 private void OnDehydrate ( IDehydrationContext context )
@@ -2023,7 +2030,7 @@ private sealed class DeactivationInfo
20232030 }
20242031 }
20252032
2026- private abstract class Command ( CancellationTokenSource cts )
2033+ private abstract class Command ( CancellationTokenSource cts ) : IDisposable
20272034 {
20282035 private bool _disposed ;
20292036 private readonly CancellationTokenSource _cts = cts ;
@@ -2040,26 +2047,41 @@ public virtual void Cancel()
20402047
20412048 public virtual void Dispose ( )
20422049 {
2043- lock ( this )
2050+ try
20442051 {
2045- _disposed = true ;
2046- _cts . Dispose ( ) ;
2052+ lock ( this )
2053+ {
2054+ _disposed = true ;
2055+ _cts . Dispose ( ) ;
2056+ }
20472057 }
2058+ catch
2059+ {
2060+ // Ignore.
2061+ }
2062+
2063+ GC . SuppressFinalize ( this ) ;
20482064 }
20492065
20502066 public sealed class Deactivate ( CancellationTokenSource cts , ActivationState previousState ) : Command ( cts )
20512067 {
20522068 public ActivationState PreviousState { get ; } = previousState ;
20532069 }
20542070
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 )
20562072 {
20572073 public Dictionary < string , object > ? RequestContext { get ; } = requestContext ;
20582074 }
20592075
20602076 public sealed class Rehydrate ( IRehydrationContext context ) : Command ( new ( ) )
20612077 {
20622078 public readonly IRehydrationContext Context = context ;
2079+
2080+ public override void Dispose ( )
2081+ {
2082+ base . Dispose ( ) ;
2083+ ( Context as IDisposable ) ? . Dispose ( ) ;
2084+ }
20632085 }
20642086
20652087 public sealed class Delay ( TimeSpan duration ) : Command ( new ( ) )
0 commit comments