Startup failure with rc2-20466 and dotnet rc2-002339 #695
Description
From @nil4 on April 10, 2016 22:23
When an exception is thrown in the constructor of a service, and that service is injected in the Configure
method, the application crashes on startup, but does not include any trace of the actual exception.
I encountered the issue in a larger project, and reduced the repro to this gist. Versions of Windows, dotnet
, dnvm
and the content of project.lock.json
are also included there. Here is the code:
sealed class Startup {
public static void Main(string[] args) => new WebHostBuilder().CaptureStartupErrors(false)
.UseKestrel().UseStartup<Startup>().Build().Run();
public void ConfigureServices(IServiceCollection services) => services.AddSingleton<Crasher>();
public void Configure(Crasher crasher) {}
}
sealed class Crasher {
public Crasher() { throw new Exception("Crash in the constructor"); }
}
Running the repro application with -f net461
prints the following to the console before the Windows error dialog (Repro.exe has stopped working) shows up:
> dotnet run -f net461
Unhandled Exception: System.Exception: Could not resolve a service of type 'Repro.Crasher' for the parameter 'crasher' of method 'Configure' on type 'Repro.Startup'.
at Microsoft.AspNetCore.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Repro.Startup.Main(String[] args)
I expected the actual exception message (Crash in the constructor) and source (Repro.Crasher
) to be shown. In the real application, it took a long time to figure out the cause of the crash.
Changing to CaptureStartupErrors(true)
prints the same error message, but the application does not crash; it starts listening on http://localhost:5000. However, the startup error page also does not include any details of the actual exception; the only thing it shows is:
Oops. 500 Internal Server Error
An error occurred while starting the application.
Trying to run the same repro with -f netcoreapp1.0
fails with Could not resolve coreclr path
. I know things are still in flux with the new TFM, so I thought I should mention this also, just in case I missed something obvious in my project.json
.
Copied from original issue: dotnet/aspnetcore#1384