@@ -152,18 +152,34 @@ public void Dispose()
152
152
_nativeApplication . Dispose ( ) ;
153
153
}
154
154
155
+ private void IncrementRequests ( )
156
+ {
157
+ Interlocked . Increment ( ref _outstandingRequests ) ;
158
+ }
159
+
160
+ internal void DecrementRequests ( )
161
+ {
162
+ if ( Interlocked . Decrement ( ref _outstandingRequests ) == 0 && Stopping )
163
+ {
164
+ // All requests have been drained.
165
+ _nativeApplication . StopCallsIntoManaged ( ) ;
166
+ _shutdownSignal . TrySetResult ( null ) ;
167
+ }
168
+ }
169
+
155
170
private static NativeMethods . REQUEST_NOTIFICATION_STATUS HandleRequest ( IntPtr pInProcessHandler , IntPtr pvRequestContext )
156
171
{
157
172
IISHttpServer server = null ;
158
173
try
159
174
{
160
175
// Unwrap the server so we can create an http context and process the request
161
176
server = ( IISHttpServer ) GCHandle . FromIntPtr ( pvRequestContext ) . Target ;
162
- Interlocked . Increment ( ref server . _outstandingRequests ) ;
177
+ server . IncrementRequests ( ) ;
163
178
164
179
var context = server . _iisContextFactory . CreateHttpContext ( pInProcessHandler ) ;
165
180
166
- ThreadPool . QueueUserWorkItem ( state => _ = HandleRequest ( ( IISHttpContext ) state ) , context ) ;
181
+ ThreadPool . UnsafeQueueUserWorkItem ( context , preferLocal : false ) ;
182
+
167
183
return NativeMethods . REQUEST_NOTIFICATION_STATUS . RQ_NOTIFICATION_PENDING ;
168
184
}
169
185
catch ( Exception ex )
@@ -174,23 +190,6 @@ private static NativeMethods.REQUEST_NOTIFICATION_STATUS HandleRequest(IntPtr pI
174
190
}
175
191
}
176
192
177
- private static async Task HandleRequest ( IISHttpContext context )
178
- {
179
- bool successfulRequest = false ;
180
- try
181
- {
182
- successfulRequest = await context . ProcessRequestAsync ( ) ;
183
- }
184
- catch ( Exception ex )
185
- {
186
- context . Server . _logger . LogError ( 0 , ex , $ "Unexpected exception in { nameof ( IISHttpServer ) } .{ nameof ( HandleRequest ) } .") ;
187
- }
188
- finally
189
- {
190
- CompleteRequest ( context , successfulRequest ) ;
191
- }
192
- }
193
-
194
193
private static bool HandleShutdown ( IntPtr pvRequestContext )
195
194
{
196
195
IISHttpServer server = null ;
@@ -238,28 +237,6 @@ private static NativeMethods.REQUEST_NOTIFICATION_STATUS OnAsyncCompletion(IntPt
238
237
}
239
238
}
240
239
241
- private static void CompleteRequest ( IISHttpContext context , bool result )
242
- {
243
- // Post completion after completing the request to resume the state machine
244
- context . PostCompletion ( ConvertRequestCompletionResults ( result ) ) ;
245
-
246
- if ( Interlocked . Decrement ( ref context . Server . _outstandingRequests ) == 0 && context . Server . Stopping )
247
- {
248
- // All requests have been drained.
249
- context . Server . _nativeApplication . StopCallsIntoManaged ( ) ;
250
- context . Server . _shutdownSignal . TrySetResult ( null ) ;
251
- }
252
-
253
- // Dispose the context
254
- context . Dispose ( ) ;
255
- }
256
-
257
- private static NativeMethods . REQUEST_NOTIFICATION_STATUS ConvertRequestCompletionResults ( bool success )
258
- {
259
- return success ? NativeMethods . REQUEST_NOTIFICATION_STATUS . RQ_NOTIFICATION_CONTINUE
260
- : NativeMethods . REQUEST_NOTIFICATION_STATUS . RQ_NOTIFICATION_FINISH_REQUEST ;
261
- }
262
-
263
240
private class IISContextFactory < T > : IISContextFactory
264
241
{
265
242
private readonly IHttpApplication < T > _application ;
0 commit comments