Load host IConfiguration from supplied command line args;

Viewed 237

ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management by auto-generating a “Program.cs”, which takes care of setting up a host. According to the documentation, if you use CreateDefaultBuilder, the following defaults are applied to HostBuilder:

  • load host IConfiguration from supplied command line args;
  • load app IConfiguration from environment variables;

Does anyone have information on how the arguments affect the CreateDefaultBuilder? If I pass arguments on the following code, How can I access that?

static void Main(string[] args) { IHostBuilder common= Host.CreateDefaultBuilder(args) }

1 Answers

I just found out how to use IConfiguration. If you Inject "foo:123" on the arguments, you could store it this way:

   IConfiguration Configuration = new ConfigurationBuilder()


  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

               .AddEnvironmentVariables()
               .AddCommandLine(args)
               .Build();
   var customSection = Configuration.GetSection("foo");
Related