Reputation: 3259
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; }
}
Upvotes: 112
Views: 148687
Reputation: 64269
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();
}
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();
}
Upvotes: 123
Reputation: 2791
.NET 8
Given appsettings.json
file:
{
"MySettings": {
"Name": "Bob",
"Counter": "100",
}
}
And MySettings
class:
public class MySettings
{
public string? Name { get; init; }
public int? Counter { get; init; }
}
Read those values in Program.cs
file:
var builder = WebApplication.CreateBuilder(args);
var mySettings = new MySettings();
builder.Configuration.GetRequiredSection(nameof(MySettings)).Bind(mySettings);
var name = mySettings.Name; // Bob
var counter = mySettings.Counter; // 100
Upvotes: 4
Reputation: 3635
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");
Upvotes: 3
Reputation: 3057
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"];
Upvotes: 148
Reputation: 1204
An old question with some new answers, so this is how I like to do it.
Define the config in appsettings.json
"ServiceConfig": { "PortNumber": 5005 },
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; }
}
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>();
Upvotes: 3
Reputation: 1458
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;
}
}
Upvotes: 2
Reputation: 49
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"];
}
}
Upvotes: -3
Reputation: 38079
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>();
});
}
Upvotes: 4
Reputation: 6337
.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);
}
Upvotes: 5
Reputation: 7
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();
}
Upvotes: -4