How to get a config value from appsettings.json early in Program class?

112 Views Asked by At

I need to read a configuration value early in the CreateHostBuilder(string[] args) method as shown in code below. But my problem is, I couldn't figure out how to get to the Configuration object in this place, this early in the process. How do I do that? Or is it possible to do?

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        var builder = Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

        // Need to read a configuration setting from appsettings.json here, such as
        // var value = Configuration["MyValue"];
        ...
    }
}
0

There are 0 best solutions below