WebHost needs an error flag
When the setting CaptureStartupErrors is set to True and you do Db initialization after building the WebHost there is no way to tell if an exception has occured during configuration. In my case I am getting a service from WebHost.Services which throws an exception because it doesn't exist. The end result is not seeing the configuration exception
@divega @ajcvickers This is a result of doing more things in Program.Main instead of Startup. That change that we made to the pattern in 2.1 means that you don't catch these errors anymore. Do you guys have any thoughts on this?
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSetting("detailedErrors", "true")
.CaptureStartupErrors(true)
.UseKestrel(options =>
{
options.AddServerHeader = false;
}
)
.UseAutofac()
.UseConfiguration(Configuration)
.UseSerilog()
.UseStartup<TStartup>();
var host = CreateWebHostBuilder(args).Build();
//Db initialization
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
//Throws Exception
var taskRunner = services.GetRequiredService<TaskRunner>();
}
host.Run();
For the time being will use services.GetService instead and check for null but would be nice to have a flag
@davidfowl he's not asking about catching the EF error but being able to see the underlying Startup error, or at least see that there was one captured.
I see, I misunderstood. We could store the exception and expose it on a service I guess. Is that what you're asking for? Why are you using CaptureStartupErrors in the first place? Why not let it throw and catch yourself.
@davidfowl I do that for unhandled exceptions while the host is running but I like the idea of displaying the error page for startup configuration exceptions
So you just want access to the exception so you can log it again? (The web host already logs it). Or do you just want to use it so you can avoid the EF code ?
Use it to avoid EF Code