@@ -6,6 +6,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core;
66internal sealed class IISNativeApplication
77{
88 private readonly NativeSafeHandle _nativeApplication ;
9+ private bool _hasRegisteredCallbacks ;
910 private readonly object _sync = new object ( ) ;
1011
1112 public IISNativeApplication ( NativeSafeHandle nativeApplication )
@@ -24,14 +25,22 @@ public void StopIncomingRequests()
2425 }
2526 }
2627
27- public void StopCallsIntoManaged ( )
28+ public void Stop ( )
2829 {
2930 lock ( _sync )
3031 {
31- if ( ! _nativeApplication . IsInvalid )
32+ if ( _nativeApplication . IsInvalid )
33+ {
34+ return ;
35+ }
36+
37+ if ( _hasRegisteredCallbacks )
3238 {
3339 NativeMethods . HttpStopCallsIntoManaged ( _nativeApplication ) ;
3440 }
41+
42+ _nativeApplication . Dispose ( ) ;
43+ GC . SuppressFinalize ( this ) ;
3544 }
3645 }
3746
@@ -44,6 +53,8 @@ public unsafe void RegisterCallbacks(
4453 IntPtr pvRequestContext ,
4554 IntPtr pvShutdownContext )
4655 {
56+ _hasRegisteredCallbacks = true ;
57+
4758 NativeMethods . HttpRegisterCallbacks (
4859 _nativeApplication ,
4960 requestCallback ,
@@ -55,20 +66,9 @@ public unsafe void RegisterCallbacks(
5566 pvShutdownContext ) ;
5667 }
5768
58- public void Dispose ( )
59- {
60- lock ( _sync )
61- {
62- GC . SuppressFinalize ( this ) ;
63-
64- // Don't need to await here because pinvokes should never been called after disposing the safe handle.
65- _nativeApplication . Dispose ( ) ;
66- }
67- }
68-
6969 ~ IISNativeApplication ( )
7070 {
7171 // If this finalize is invoked, try our best to block all calls into managed.
72- StopCallsIntoManaged ( ) ;
72+ Stop ( ) ;
7373 }
7474}
0 commit comments