Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle debug/release config transformations in ASP.NET vNext

Tags:

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?

like image 503
2ooom Avatar asked Oct 20 '14 14:10

2ooom


1 Answers

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); 
like image 113
govin Avatar answered Sep 20 '22 21:09

govin