@@ -60,7 +60,7 @@ public AspNetProcess(
6060 Process = ProcessEx . Run ( output , workingDirectory , DotNetMuxer . MuxerPathOrDefault ( ) , arguments , envVars : environmentVariables ) ;
6161 if ( hasListeningUri )
6262 {
63- ListeningUri = GetListeningUri ( output ) ;
63+ ListeningUri = GetListeningUri ( output ) ?? throw new InvalidOperationException ( "Couldn't find the listening URL." ) ;
6464 }
6565 }
6666
@@ -175,16 +175,15 @@ private Uri GetListeningUri(ITestOutputHelper output)
175175 {
176176 // Wait until the app is accepting HTTP requests
177177 output . WriteLine ( "Waiting until ASP.NET application is accepting connections..." ) ;
178- var listeningMessage = Process
179- . OutputLinesAsEnumerable
180- . Where ( line => line != null )
181- . FirstOrDefault ( line => line . Trim ( ) . StartsWith ( ListeningMessagePrefix , StringComparison . Ordinal ) ) ;
178+ var listeningMessage = GetListeningMessage ( ) ;
182179
183180 if ( ! string . IsNullOrEmpty ( listeningMessage ) )
184181 {
185182 listeningMessage = listeningMessage . Trim ( ) ;
186183 // Verify we have a valid URL to make requests to
187- var listeningUrlString = listeningMessage . Substring ( ListeningMessagePrefix . Length ) ;
184+ var listeningUrlString = listeningMessage . Substring ( listeningMessage . IndexOf (
185+ ListeningMessagePrefix , StringComparison . Ordinal ) + ListeningMessagePrefix . Length ) ;
186+
188187 output . WriteLine ( $ "Detected that ASP.NET application is accepting connections on: { listeningUrlString } ") ;
189188 listeningUrlString = listeningUrlString . Substring ( 0 , listeningUrlString . IndexOf ( ':' ) ) +
190189 "://localhost" +
@@ -199,6 +198,25 @@ private Uri GetListeningUri(ITestOutputHelper output)
199198 }
200199 }
201200
201+ private string GetListeningMessage ( )
202+ {
203+ try
204+ {
205+ return Process
206+ // This will timeout at most after 5 minutes.
207+ . OutputLinesAsEnumerable
208+ . Where ( line => line != null )
209+ // This used to do StartsWith, but this is less strict and can prevent issues (very rare) where
210+ // console logging interleaves with other console output in a bad way. For example:
211+ // dbugNow listening on: http://127.0.0.1:12857
212+ . FirstOrDefault ( line => line . Trim ( ) . Contains ( ListeningMessagePrefix , StringComparison . Ordinal ) ) ;
213+ }
214+ catch ( OperationCanceledException )
215+ {
216+ return null ;
217+ }
218+ }
219+
202220 private bool IsSuccessStatusCode ( HttpResponseMessage response )
203221 {
204222 return response . IsSuccessStatusCode || response . StatusCode == HttpStatusCode . Redirect ;
0 commit comments