Detailed Explanation of Flexible Configuration Mode in asp. net core
- 2021-10-11 18:00:52
- OfStack
Preface
asp. net core supports external files and command line parameters to configure the configuration information required for system operation. Let's talk about the specific usage methods from the following two common scenarios.
1. Listening address and port configuration
1. Command line mode
The asp. net core system starts from the command line using the following commands:
dotnet run
The above command is executed directly in the source code directory, and the program can be compiled and run. For programs that have been released, you can't use the above instructions, but you should use the following instructions:
dotnet assembly file name (the assembly file name is the dll file generated after the program is published)
The above two instructions can start the application. After the program starts, the default listening address and port is http://localhost: 5000. However, after the program is released, it will definitely not use the default address and port. What if you want to listen to other addresses or domain names when you start? The answer is to use the-urls parameter, and the specific instruction format is as follows:
dotnet run --urls="http://域名:端口号"
If you want to set up multiple domain names, you can use semicolons to separate them.
By default, the program does not support parameter passing, so we need to configure the program. First of all, you need to introduce
Microsoft.Extensions.Configuration.CommandLine
Library file, and then add commandline support to the main method, as follows:
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)// Increase commandline Support
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
After configuration, you can use the above instructions to pass parameters
2. Configuration file
asp. net core configuration information can also be placed in a configuration file, and the contents of the configuration file are loaded when the system starts to affect the environment parameters required for program startup. Let's look at the specific operation process.
First, we need to introduce a library file "Microsoft. Extensions. Configuration. Json", and then introduce the configuration file path information in the main method. The specific code is as follows:
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json")
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
In the above way, we have added an external hosting. json configuration file, in which we can add listening address information, as follows:
{
"server.urls": "http://*:5001"
}
2. Runtime environment configuration
When developing a project, it is often required to separate the development environment, test environment and formal environment, and the parameters of different environments are different, such as listening address, database connection information and so on. Of course, we save the configuration information to a file, Every time you publish, you can modify the contents of the configuration file first, and then publish the program, which is undoubtedly very troublesome. Every time you publish, you must first determine the corresponding environment, and then modify the configuration information. If you need to publish multiple environment versions at the same time, you have to operate many times.
asp. net core has actually considered such a scenario, so we can look at the following code first:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
The above code appears in the startup. cs file, which first uses the
AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
Load the appsettings configuration file, which can place information shared by all environments, followed by a sentence
AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
,
env.EnvironmentName
In fact, it is the system environment. According to the EnvironmentName set at startup, the corresponding configuration file contents can be loaded.
The question now is how to specify this EnvironmentName.
1. Specify environment from the command line
Before executing dotnet run, you can execute the following instructions:
set ASPNETCORE_ENVIRONMENT
= Environment name. Note that there are no quotation marks here. Just write the environment name as a specific value, such as
set ASPNETCORE_ENVIRONMNET=development
And then execute
dotnet run
Directive, so that the current run will run according to the environment set in the set directive
2. Pass specific parameters directly to dotnet run instruction
Look at the direct implementation effect first:
dotnet run --ASPNETCORE_ENVIRONMENT=development
Specific practices:
dotnet run --urls="http://域名:端口号"
0
,
Microsoft.Extensions.Configuration.EnvironmentVariables
Library file, and then add the support of environment parameters in main method. The specific code is as follows:
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json")
.Build();
var host = new WebHostBuilder()
.UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
The point is
AddEnvironmentVariables(),UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
The treatment of two places. So that we can
dotnet run
The corresponding environment parameter is added after the instruction.
Summarize