The web.config
transformation syntax is oriented on XML format of data. The new configurations consist of some files in JSON format and one can implement staging scenarios very easy.
First of all ASP.NET supports allows to set destination environment by usage ASPNET_ENV
environment variable or by setting Hosting:Environment
in launchSettings.json
file (see Properties
folder of your project). The file launchSettings.json
can be modified in Visual Studio in properties of the project. One should first choose the "Profile"
and make setting for every profile. Alternatively one can just edit the file PropertieslaunchSettings.json
manually.
Some configuration files, like hosting.json
works automatically using staging. Thus you can set for example different ports and different interface binding by specifying server.urls
in hosting.json
and hosting.Development.json
for example.
To include staging logic in appsettings.json
one need modify the constructor of Startup
class in Startup.cs
. For example:
public class Startup
{
public static IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyContext>(options => {
options.UseSqlServer(Configuration["Data:ConnectionString"]);
})
.AddDbContext<SM9Context>(options => {
options.UseSqlServer(Configuration["Data:SM9ConnectionString"]);
});
}
}
The above code saves configuration in Configuration
property and then uses ConfigureServices
to make injection of MyContext
and SM9Context
database contexts. One can for example create main appsettings.json
file with all productive configuration and create appsettings.Development.json
file which overrides only one (from two Data:ConnectionString
and Data:SM9ConnectionString
) connection string:
{
"Data": {
"ConnectionString": "Server=..."
}
}
ASP.NET will combine both files appsettings.json
and optional appsettings.Development.json
to create the full set of configuration parameters.
The article and the part of documentation describes how one can use the staging in ASP.NET 5.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…