0

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"];
        ...
    }
}
Alexu
  • 1,015
  • 2
  • 12
  • 32
  • 1
    What version of asp.net core are you using? In the 2.x version, you can use the method ConfigurationBuilder to obtain configuration information. If you are using asp.net core3.1 or higher, reading this post may help you:https://stackoverflow.com/questions/41738692/read-appsettings-json-in-main-program-cs – Tupac Jan 06 '22 at 06:19
  • Thanks. I am on Core 5. Some of the options you suggested should work, but none of them looks perfect. I realize that this is a sort of chicken-egg situation... – Alexu Jan 06 '22 at 17:41

0 Answers0