Why is that a call to IWebHostBuilder.Configure() extension method seemingly doesn't do anything in ASP.NET Core (experienced in version 3.4.0)?
For example in this scenario:
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder
.UseSerilog( ... )
.Configure(appBuilder => // Doesn't do anything.
appBuilder.UseSerilogRequestLogging( ... ))
.UseStartup<Startup>();
});
Here, UseSerilogRequestLogging() is called inside Configure() to add Serilog's request logging middleware before Startup executes, in order to place it at the beginning of the request pipeline, and also to keep logging-related configuration at one place.
But Configure() literally doesn't do anything, and the middleware is not added.
The reason is that the
IWebHostBuilder.Configure()method is not just a general configuration method. It actually registers the provided delegate asIStartupin theServiceCollection. See source (albeit old) here.This means that when
UseStartup<Startup>()is called subsequently, it replaces the previously registered delegate, and thus the configuration inConfigure()is not executed.This behavior can be further confirmed if you place
Configure()afterUseStartup<>(). In this case,Configure()will be the one that replacesUseStartup<>(), andUseStartup()won't execute.The documentation on
Configure()method actually hints at this:(Answered my own question, to spare some time for someone else who might end up being as perplexed as I was.)