92

First of all my main purpose is to setup the IP and Port for my application dynamically.

I'm using IConfiguration to inject a json config file, like some tutorial mentioned.

However, I can't retrieve the configuration in Program.cs, because my WebHostBuilder will use the StartUp and Url at the same time.

So at the time the host build up, there is nothing in my configuration.

WebProtocolSettings settings_Web = new WebProtocolSettings();
var host = new WebHostBuilder()
                .UseIISIntegration()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .UseUrls(settings_Web.Url + ":" + settings_Web.Port)
                .Build();

In Startup.cs

public Startup(IHostingEnvironment env)
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; set; }

public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();

    var _WebProtocolSettings = Configuration.GetSection("WebProtocolSettings");

    // Register the IConfiguration instance
    services.Configure<WebProtocolSettings>(_WebProtocolSettings);
}

My appsettings.json:

{
    "WebProtocolSettings": {
        "Url": "127.0.0.1",
        "Port": 5050
    }
}

My WebProtocolSettings.cs:

public class WebProtocolSettings
{
    public string Url { get; set; }
    public int Port { get; set; }
}
Jacky
  • 2,924
  • 3
  • 22
  • 34

9 Answers9

108

In .NET 6

var builder = WebApplication.CreateBuilder(args);

// Using the GetValue<type>(string key) method
var configValue = builder.Configuration.GetValue<string>("Authentication:CookieAuthentication:LoginPath");

// or using the index property (which always returns a string)
var configValue = builder.Configuration["Authentication:CookieAuthentication:LoginPath"];
user276648
  • 6,018
  • 6
  • 60
  • 86
Sukesh Chand
  • 2,339
  • 2
  • 21
  • 29
  • 7
    Thanks, exactly what I needed. Should be on the top as most other answers are outdated. – Aileron79 May 02 '22 at 14:49
  • 4
    Which is also written `var configValue = builder.Configuration["Authentication:CookieAuthentication:LoginPath"];` – Tod Jun 24 '22 at 13:50
107

Update .Net 6

It's now easy to get any settings from the ConfigurationManager by calling the GetValue<type>(string key) extension method. You can also use Index(string key) to return a string. See this answer.


You must build a configuration in your main method, get the section and bind it to your model. No way around it.

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: false)
        .Build();

    WebProtocolSettings settings_Web = new WebProtocolSettings();
    config.GetSection("WebProtocolSettings").Bind(settings_Web);

    var host = new WebHostBuilder()
            .UseIISIntegration()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .UseUrls(settings_Web.Url + ":" + settings_Web.Port)
            .Build()

    host.Run();
}

##Update

An alternative way of doing it is by passing the configuration to UseConfiguration as described in the

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true)
        .AddCommandLine(args)
        .Build();

    var host = new WebHostBuilder()
        .UseUrls("http://*:5000")
        .UseConfiguration(config)
        .UseKestrel()
        .Configure(app =>
        {
            app.Run(context => 
                context.Response.WriteAsync("Hello, World!"));
        })
        .Build();

    host.Run();
}

or in ASP.NET Core > 2.0

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hosting.json", optional: true)
        .AddCommandLine(args)
        .Build();

    return WebHost.CreateDefaultBuilder(args)
        .UseUrls("http://*:5000")
        .UseConfiguration(config)
        .Configure(app =>
        {
            app.Run(context => 
                context.Response.WriteAsync("Hello, World!"));
        })
        .Build();
}
user276648
  • 6,018
  • 6
  • 60
  • 86
Tseng
  • 61,549
  • 15
  • 193
  • 205
  • Hi @Tseng, thank you for your answer, however, how do I archive the path for "appsettings.json" in Main Program.cs? – Jacky Jan 19 '17 at 10:06
  • It has to be in the same folder as the application. Same rules apply when you call it within the startup class – Tseng Jan 19 '17 at 10:15
  • 1
    I found the ways: `.SetBasePath(Directory.GetCurrentDirectory())`. Also, should be `config.GetSection` not , `Configuration.GetSection`. Thank you for your help – Jacky Jan 19 '17 at 10:17
  • 4
    @Tseng what If you need to access hostingContext.HostingEnvironment.EnvironmentName because you have environment specific hosting.json files? – Learner Dec 18 '18 at 01:31
  • Note that when configured this way the configuration won't be updated when the JSON files are changed at runtime. I posted an alternative that works around this. – EM0 May 17 '19 at 09:54
  • To add to the complexity of the question, how does it read the configuration specific to the environment, appsettings-${ENVIRONMENT}.json? – Kok How Teh May 28 '19 at 09:03
  • Will this solution still work when deployed. For example will the app settings in the Json be overridden by the app settings found in the app service? – Dan Cundy Mar 31 '20 at 11:40
  • 1
    @DanCundy: (Azure) App Service values are set via environment variables, so you would have to add `.AddEnvironmentVariables()`. The order of the `AddXxx` is the order in which will be overridden (latest key wins, so what ever is registered last will override previous one - if it has a specific key defined). But these days you have [Generic Host](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1) for easy bootstrapping of console running services – Tseng Apr 01 '20 at 08:32
5

.UseConfiguration (Tseng's alternative answer) is the simplest way, but note that when configured this way changes made to the configuration files at runtime are not applied to your IConfiguration objects. To keep the configuration dynamic you have to use .ConfigureAppConfiguration - but then you have to build the configuration an extra time for use in Main(). You can re-use the code that configures it, however.

ASP.NET Core 2.2:

    public static void Main(string[] args)
    {
        IConfigurationBuilder configBuilderForMain = new ConfigurationBuilder();
        ConfigureConfiguration(configBuilderForMain);
        IConfiguration configForMain = configBuilderForMain.Build();

        // ... use configForMain to read config here ...

        var host = new WebHostBuilder()
            .ConfigureAppConfiguration(ConfigureConfiguration)
            // ... the rest of it ...
            .Build();
    }

    public static void ConfigureConfiguration(IConfigurationBuilder config)
    {
        config.SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    }
EM0
  • 5,369
  • 7
  • 51
  • 85
4

In asp.net core 3.1, you can access configuration through the hostContext. This sample is based on the Worker Service Project:

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

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    //Access configuration here with the Host Context.  For example, to get a connection string from AppSettings.json:
                    var connectionString = hostContext.Configuration.GetConnectionString("MyConnectionString");
                    services.AddHostedService<Worker>();
                });
    }
Greg Gum
  • 33,478
  • 39
  • 162
  • 233
3

An old question with some new answers, so this is how I like to do it.

  1. Define the config in appsettings.json

         "ServiceConfig": { "PortNumber": 5005 },
    
  2. Create a class for the config:

         public class ServiceConfig
         {
             private readonly IConfiguration configuration;
             public const string SectionName = "ServiceConfig";
             public ServiceConfig(IConfiguration config)
             {
                 configuration = config;            
                 configuration.GetSection(SectionName).Bind(this);            
             }
             public int PortNumber { get; set; }
         }
    
  3. In Program.cs use the config:

         var config = new ServiceConfig(builder.Configuration);
         builder.WebHost.UseUrls($"http://*:{config.PortNumber}");
    

This method has the added benefit of being able to be used as a service too for dependency injection:

         builder.Services.AddSingleton<ServiceConfig>();
Joe Bourne
  • 1,144
  • 10
  • 18
1

The method with the support for command line arguments and default appsettings files:

public static class Configuration
{
    public static IConfigurationRoot BuildConfigurationRoot()
    {

        var args = Environment.GetCommandLineArgs();
        var envArg = Array.IndexOf(args, "--environment");
        var envFromArgs = envArg >= 0 ? args[envArg + 1] : null;

        var aspnetcore = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        var dotnetcore = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");

        var environment = envFromArgs ?? (string.IsNullOrWhiteSpace(aspnetcore)
            ? dotnetcore
            : aspnetcore);

        var configuration = new ConfigurationBuilder()
            .AddCommandLine(Environment.GetCommandLineArgs())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile(
                $"appsettings.{environment}.json",
                optional: true)
            .Build();

        return configuration; 
    }
}
Stack Undefined
  • 1,050
  • 1
  • 14
  • 23
Oleg
  • 1,378
  • 11
  • 22
  • Might you want to include ".AddEnvironmentVariables()" as a line prior to .Build() ? – Dale Holborow Sep 18 '21 at 08:57
  • There is a minor error with the `IndexOf()` method. This method is a static method in the `Array` class. So instead of `args.IndexOf()`, you need to use the `Array.IndexOf("--environment");`. An instance of an array cannot be used to access the `IndexOf` static method. – Stack Undefined May 31 '22 at 04:01
0

In .NET 7 you need to use the BuildServiceProvider()

So the top of your program.cs will look something like this;

var builder = WebApplication.CreateBuilder(args);
var provider = builder.Services.BuildServiceProvider();
var _configuration = provider.GetRequiredService<IConfiguration>();

And then you can access your appsettings.json values like so;

var configVal = _configuration.GetSection("Foo").GetValue<string>("Bar");
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
-2

In .net core 3.1 you can use ConfigurationHelper.GetConfiguration() to get appSetting variables:

appSettings.json

"Endpoint": {
"ip": "170.888.88.888",
"port": 88888}

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
     var config = ConfigurationHelper.GetConfiguration();
     var ip = config["Endpoint:ip"];
     var port = config["Endpoint:port"];
    }
 }
Suman George
  • 51
  • 3
  • 9
  • 2
    Where does "ConfigurationHelper" come from? I see some examples of custom classes with this name but behind the scenes they do the same thing as the accepted answer. – k3davis May 19 '21 at 10:50
-3

Use this code:

public class Program
{
    private static string _environmentName;

    public static void Main(string[] args)
    {
        var webHost = BuildWebHost(args);

        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true)
            .AddCommandLine(args)
            .Build();

        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, config) =>
    {
        config.ClearProviders();
        _environmentName = hostingContext.HostingEnvironment.EnvironmentName;
    }).UseStartup<Startup>().Build();
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29