@@ -635,6 +635,15 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
635635 // Run the application code for this request
636636 await application . ProcessRequestAsync ( context ) ;
637637
638+ // Trigger OnStarting if it hasn't been called yet and the app hasn't
639+ // already failed. If an OnStarting callback throws we can go through
640+ // our normal error handling in ProduceEnd.
641+ // https://github.com/aspnet/KestrelHttpServer/issues/43
642+ if ( ! HasResponseStarted && _applicationException == null && _onStarting ? . Count > 0 )
643+ {
644+ await FireOnStarting ( ) ;
645+ }
646+
638647 if ( ! _connectionAborted && ! VerifyResponseContentLength ( out var lengthException ) )
639648 {
640649 ReportApplicationError ( lengthException ) ;
@@ -655,15 +664,6 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
655664
656665 KestrelEventSource . Log . RequestStop ( this ) ;
657666
658- // Trigger OnStarting if it hasn't been called yet and the app hasn't
659- // already failed. If an OnStarting callback throws we can go through
660- // our normal error handling in ProduceEnd.
661- // https://github.com/aspnet/KestrelHttpServer/issues/43
662- if ( ! HasResponseStarted && _applicationException == null && _onStarting ? . Count > 0 )
663- {
664- await FireOnStarting ( ) ;
665- }
666-
667667 // At this point all user code that needs use to the request or response streams has completed.
668668 // Using these streams in the OnCompleted callback is not allowed.
669669 StopBodies ( ) ;
@@ -942,7 +942,7 @@ public void ProduceContinue()
942942 }
943943 }
944944
945- public Task InitializeResponseAsync ( int firstWriteByteCount , bool appCompleted = false )
945+ public Task InitializeResponseAsync ( int firstWriteByteCount )
946946 {
947947 var startingTask = FireOnStarting ( ) ;
948948 // If return is Task.CompletedTask no awaiting is required
@@ -953,7 +953,7 @@ public Task InitializeResponseAsync(int firstWriteByteCount, bool appCompleted =
953953
954954 VerifyInitializeState ( firstWriteByteCount ) ;
955955
956- ProduceStart ( appCompleted : appCompleted ) ;
956+ ProduceStart ( appCompleted : false ) ;
957957
958958 return Task . CompletedTask ;
959959 }
@@ -1050,7 +1050,7 @@ protected Task ProduceEnd()
10501050 return WriteSuffix ( ) ;
10511051 }
10521052
1053- protected Task WriteSuffix ( )
1053+ private Task WriteSuffix ( )
10541054 {
10551055 if ( HasResponseCompleted )
10561056 {
0 commit comments