I try to set some configuration values during the startup before I configure Kestrel. I try to do this to use some settings from singleton and a ILogger during the configuration.
Simplified sample:
var builder = WebApplication.CreateBuilder(options);
builder.Services.Configure<MyKestrelOptions>(optionsKestrel =>
{
//Debugger nerver stops here
var configService = builder.Services.BuildServiceProvider().GetRequiredService<IConfigurationService>();
var logger = builder.Services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
KestrelStartup.BuildOptions(configService, optionsKestrel, null);
});
builder.Services.AddSingleton<IConfigurationService, ConfigurationService>();
builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
var optionsKestrel = context.Configuration.Get<KestrelOptions>();
if (optionsKestrel != null)
{
optionsKestrel.Logger?.LogDebug("DEBUG POSSIBLE");
}
});
public class KestrelOptions
{
public String? ServerPfxPath { get; set; }
public ILogger? Logger { get; set; }
}
While Debugging I never stopped in the Configure lamba. And optionsKestrel.Logger is allways null.
How to achieve this as I need some configuration settings to be read in ConfigurationService for configure Kestrel and what to log during some callbacks (ClientCertificateValidation) configured in builder.WebHost.ConfigureKestrel.