So my question is, how would I access the configuration before building the builder in .NET 6?
In .NET 5, we had Startup.cs broken up into three functions:
- public Startup()
- public void ConfigureServices(IServiceCollection services)
- public void Configure(IApplicationBuilder app)
With this setup, I was able to declare a private readonly variable to store and use the configuration throughout Startup.cs
private readonly IConfigurationRoot _config; // The variable in question.
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
_config = builder.Build(); // Initializing the configuration to be access in ConfigureServices()
}
I would use that _config variable often while setting up the services... for example:
// Example .NET 5 ConfigureServices.
services.AddSession(options =>
{
options.Cookie.Name = _config["Auth:AzureAd:SessionCookieName"];
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
However, the only way I know the access the configuration in Program.cs in .NET 6 is after creating the WebApplication builder var app = builder.Build();
The issue is, you cannot modify services after building the builder. So I have all of these service configurations before this line of code that requires access to appsettings/the configuration file.
So my question is, how would I access the configuration before building the builder in .NET 6?