Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 225 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatically set appsettings.json for dev and release environments in asp.net core?

#1
I've defined some values in my `appsettings.json` for things like database connection strings, webapi locations and the like which are different for development, staging and live environments.

Is there a way to have multiple `appsettings.json` files (like `appsettings.live.json`, etc, etc) and have the asp.net app just 'know' which one to use based on the build configuration it's running?
Reply

#2
You can make use of environment variables and the `ConfigurationBuilder` class in your `Startup` constructor like this:

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.configuration = builder.Build();
}

Then you create an `appsettings.xxx.json` file for every environment you need, with "xxx" being the environment name. Note that you can put all global configuration values in your "normal" `appsettings.json` file and only put the environment specific stuff into these new files.

Now you only need an environment variable called `ASPNETCORE_ENVIRONMENT` with some specific environment value ("live", "staging", "production", whatever). You can specify this variable in your project settings for your development environment, and of course you need to set it in your staging and production environments also. The way you do it there depends on what kind of environment this is.

**UPDATE:** I just realized you want to choose the `appsettings.xxx.json` based on your current *build configuration*. This cannot be achieved with my proposed solution and I don't know if there is a way to do this. The "environment variable" way, however, works and might as well be a good alternative to your approach.
Reply

#3
Just an update for .NET core 2.0 users, you can specify application configuration after the call to `CreateDefaultBuilder`:

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

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ConfigConfiguration)
.UseStartup<Startup>()
.Build();

static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config)
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", optional: false, reloadOnChange: true)
.AddJsonFile($"config.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);

}
}
Reply

#4
You may use conditional compilation:

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
#if SOME_BUILD_FLAG_A
.AddJsonFile($"appsettings.flag_a.json", optional: true)
#else
.AddJsonFile($"appsettings.no_flag_a.json", optional: true)
#endif
.AddEnvironmentVariables();
this.configuration = builder.Build();
}
Reply

#5
You can add the configuration name as the `ASPNETCORE_ENVIRONMENT` in the `launchSettings.json` as below


"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58446/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"environmentVariables": {
ASPNETCORE_ENVIRONMENT": "$(Configuration)"
}
}
}

Reply

#6
1. Create multiple <code>appSettings.<i>$(Configuration)</i>.json</code> files like:

* `appSettings.staging.json`
* `appSettings.production.json`

2. Create a pre-build event on the project which copies the respective file to `appSettings.json`:

```bash
copy appSettings.$(Configuration).json appSettings.json
```

3. Use only `appSettings.json` in your Config Builder:

```cs
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();

Configuration = builder.Build();
```

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through