How to handle debug/release config transformations

2019-03-09 18:35发布

In previous versions of ASP.NET many of us used Web.Debug.config/Web.Release.config files trasformations that would look something like this:

Web.config:

<connectionStrings>
  <add name="AppDB" connectionString="Data Source=(LocalDb)\\..." />
</connectionStrings>

Web.Release.config:

<connectionStrings>
  <add name="AppDB" connectionString="Data Source=(ReleaseDb)\\..."  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

As per ASP.NET vNext tutorial you still can use Web.config. But config.json appear to be the new way to handle configurations now as per the same article:

config.json

{
    "Data": {
        "DefaultConnection": { 
            "ConnectionString": "Server=(localdb)\\..."
        }
    }
}

And in Startup.cs:

var configuration = new Configuration();
configuration.AddJsonFile("config.json");
configuration.AddEnvironmentVariables();

So I'm wondering what would be the suggested way to handle config-transofrmation with this shift to json?

3条回答
2楼-- · 2019-03-09 18:56

vNext uses a new configuration system in which you can read the environment variables through code. So, in this case you would check for the presence of the appropriate environment variable and include the corresponding JSON through code.

For example, you can create a qa.json and prod.json. Set an environment variable, say, "ENV" that points to "qa" and "prod" in those respective environments. Then conditionally you can add the appropriate JSON.

The code could look like this:

1) default.json contains all your default stuff.

2) qa.json and prod.json contain the necessary overrides.

3) Since qa.json and prod.json come later, they will win. If there is a "setting1" in default.json and qa.json, it will automatically pick up the "setting1" in qa.json

 var configuration = new Configuration()
                     .AddJsonFile("default.json")
                     .AddEnvironmentVariables(); 

 var envSpecificJson = configuration.Get("ENV") + ".json";
 configuration.AddJsonFile(envSpecificJson);
查看更多
你好瞎i
3楼-- · 2019-03-09 19:02

By default, ASP.NET will check for an environment variable named "ASPNET_ENV" to set IHostingEnvironment's EnvironmentName variable.

The relevant code can be seen here:

https://github.com/aspnet/Hosting/blob/77e2dc263f11655312d4c73bb8e22d7b6254d485/src/Microsoft.AspNet.Hosting/HostingFactory.cs

You could do something like the following if you change ASPNET_ENV's value on the servers in your various environments:

var configuration = new Configuration()
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
if (string.Equals(env.EnvironmentName, "Prod", StringComparison.OrdinalIgnoreCase))
{
            configuration.AddJsonFile("prod.config.json");
}
查看更多
何必那么认真
4楼-- · 2019-03-09 19:13

While the previous answer(s) offers a good solution I would like to present an alternate one:

Code

var configuration = new Configuration()
                     .AddJsonFile("config.json")
                     .AddEnvironmentVariables();

Local environment: Here you have the "config.json" file with all the settings. It is easily edited and maintained.

Prod environment: When deploying you omit the "config.json" file. As it is missing, the code will look for the next source in the chain which is the environment variables. Thus, when on your local machine, use the config file. When on the server, fall back to the environment variables. Note that the config keys must obviously be named the same in the file and the environment variables.

查看更多
登录 后发表回答