This repository was archived by the owner on Dec 19, 2018. It is now read-only.
File tree 2 files changed +47
-3
lines changed
src/Microsoft.AspNetCore.Hosting/Internal
test/Microsoft.AspNetCore.Hosting.Tests
2 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -191,21 +191,26 @@ private RequestDelegate BuildApplication()
191
191
192
192
return builder . Build ( ) ;
193
193
}
194
- catch ( Exception ex ) when ( _options . CaptureStartupErrors )
194
+ catch ( Exception ex )
195
195
{
196
196
// EnsureApplicationServices may have failed due to a missing or throwing Startup class.
197
197
if ( _applicationServices == null )
198
198
{
199
199
_applicationServices = _applicationServiceCollection . BuildServiceProvider ( ) ;
200
200
}
201
201
202
- EnsureServer ( ) ;
203
-
204
202
// Write errors to standard out so they can be retrieved when not in development mode.
205
203
Console . Out . WriteLine ( "Application startup exception: " + ex . ToString ( ) ) ;
206
204
var logger = _applicationServices . GetRequiredService < ILogger < WebHost > > ( ) ;
207
205
logger . ApplicationError ( ex ) ;
208
206
207
+ if ( ! _options . CaptureStartupErrors )
208
+ {
209
+ throw ;
210
+ }
211
+
212
+ EnsureServer ( ) ;
213
+
209
214
// Generate an HTML error page.
210
215
var hostingEnv = _applicationServices . GetRequiredService < IHostingEnvironment > ( ) ;
211
216
var showDetailedErrors = hostingEnv . IsDevelopment ( ) || _options . DetailedErrors ;
Original file line number Diff line number Diff line change @@ -851,6 +851,45 @@ public async Task Build_DoesNotThrowIfUnloadableAssemblyNameInHostingStartupAsse
851
851
}
852
852
}
853
853
854
+ [ Fact ]
855
+ public void StartupErrorsAreLoggedIfCaptureStartupErrorsIsTrue ( )
856
+ {
857
+ var builder = CreateWebHostBuilder ( )
858
+ . CaptureStartupErrors ( true )
859
+ . Configure ( app =>
860
+ {
861
+ throw new InvalidOperationException ( "Startup exception" ) ;
862
+ } )
863
+ . UseServer ( new TestServer ( ) ) ;
864
+
865
+ using ( var host = ( WebHost ) builder . Build ( ) )
866
+ {
867
+ var sink = host . Services . GetRequiredService < ITestSink > ( ) ;
868
+ Assert . True ( sink . Writes . Any ( w => w . Exception ? . Message == "Startup exception" ) ) ;
869
+ }
870
+ }
871
+
872
+ [ Fact ]
873
+ public void StartupErrorsAreLoggedIfCaptureStartupErrorsIsFalse ( )
874
+ {
875
+ ITestSink testSink = null ;
876
+
877
+ var builder = CreateWebHostBuilder ( )
878
+ . CaptureStartupErrors ( false )
879
+ . Configure ( app =>
880
+ {
881
+ testSink = app . ApplicationServices . GetRequiredService < ITestSink > ( ) ;
882
+
883
+ throw new InvalidOperationException ( "Startup exception" ) ;
884
+ } )
885
+ . UseServer ( new TestServer ( ) ) ;
886
+
887
+ Assert . Throws < InvalidOperationException > ( ( ) => builder . Build ( ) ) ;
888
+
889
+ Assert . NotNull ( testSink ) ;
890
+ Assert . True ( testSink . Writes . Any ( w => w . Exception ? . Message == "Startup exception" ) ) ;
891
+ }
892
+
854
893
[ Fact ]
855
894
public void HostingStartupTypeCtorThrowsIfNull ( )
856
895
{
You can’t perform that action at this time.
0 commit comments