ASP.NET Core specified environment publishing (hosting environment) ASP.NET Core specified environment publishing (hosting environment)

ASP.NET Core specified environment publishing (hosting environment)

 

ASP.NET Core application publish command:

dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]

Issue an example command (generated under the bin/release/netcoreapp1.1/publishdirectory):

dotnet publish -c release

The above command does not specify EnvironmentNamethe release, what does it mean? For example, the configuration in the ASP.NET Core application, the appsettings.jsonconfiguration of the test environment and the production environment are different (such as the database connection string). If the above command is used, we also need to manually copy the appsettings.jsonfiles in different environments, which will be changed later. , and it needs to be updated again, which is very troublesome.

How to solve the above problem is very simple. Specify the environment variable of the development machine or server. ASPNETCORE_ENVIRONMENTAfter setting the environment variable, when the dotnet *.dllstartup program is executed, ASP.NET Core will automatically load the appsettings.*.jsonfile corresponding to this environment variable, for example appsettings.Production.json.

In fact, when we use VS 2017 F5 to debug the project, the environment variables are also set by default , such as the example configuration ASPNETCORE_ENVIRONMENTin the ASP.NET Core application :launchSettings.json

"profiles": {
    "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "AspNetCore.Samples": { "commandName": "Project", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:59522" } }

StartupExample configuration:

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(); }

Because in the above configuration , we ASPNETCORE_ENVIRONMENTare Developmentusing VS 2017 F5 to debug the project, and the appsettings.Development.jsonconfiguration file under the project will be loaded and used. If this file does not exist, ASP.NET Core will use the appsettings.jsonconfiguration file by default.

So how do we set ASPNETCORE_ENVIRONMENTenvironment variables on the server? It's as simple as typing a command.

1. Windows Server Setup

Command Line:

>setx ASPNETCORE_ENVIRONMENT "Development"

SUCCESS: Specified value was saved.

or (requires admin rights)

>setx ASPNETCORE_ENVIRONMENT "Development" /M

SUCCESS: Specified value was saved.

PowerShellOrder:

$Env:ASPNETCORE_ENVIRONMENT = "Prodction"

After Windows sets the environment command, you need to re-open a command line dotnet *.dllstartup item to be effective.

2. MacOS/Linux server setup

Command Line:

export ASPNETCORE_ENVIRONMENT=development 

dotnet *.dllWhen starting the project, we can see the current one Hosting environmentto check if it is correct, example:

> dotnet AspNetCore.Samples.dll
Hosting environment: Prodtction
Content root path: C:\Users\yuezh\Desktop\Demo\AspNetCore.Samples
Now listening on: http://*:5003 Application started. Press Ctrl+C to shut down.

References:

ASP.NET Core application publish command:

dotnet publish [<PROJECT>] [-f|--framework] [-r|--runtime] [-o|--output] [-c|--configuration] [--version-suffix] [-v|--verbosity] [-h|--help]

Issue an example command (generated under the bin/release/netcoreapp1.1/publishdirectory):

dotnet publish -c release

The above command does not specify EnvironmentNamethe release, what does it mean? For example, the configuration in the ASP.NET Core application, the appsettings.jsonconfiguration of the test environment and the production environment are different (such as the database connection string). If the above command is used, we also need to manually copy the appsettings.jsonfiles in different environments, which will be changed later. , and it needs to be updated again, which is very troublesome.

How to solve the above problem is very simple. Specify the environment variable of the development machine or server. ASPNETCORE_ENVIRONMENTAfter setting the environment variable, when the dotnet *.dllstartup program is executed, ASP.NET Core will automatically load the appsettings.*.jsonfile corresponding to this environment variable, for example appsettings.Production.json.

In fact, when we use VS 2017 F5 to debug the project, the environment variables are also set by default , such as the example configuration ASPNETCORE_ENVIRONMENTin the ASP.NET Core application :launchSettings.json

"profiles": {
    "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "AspNetCore.Samples": { "commandName": "Project", "launchBrowser": true, "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:59522" } }

StartupExample configuration:

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(); }

Because in the above configuration , we ASPNETCORE_ENVIRONMENTare Developmentusing VS 2017 F5 to debug the project, and the appsettings.Development.jsonconfiguration file under the project will be loaded and used. If this file does not exist, ASP.NET Core will use the appsettings.jsonconfiguration file by default.

So how do we set ASPNETCORE_ENVIRONMENTenvironment variables on the server? It's as simple as typing a command.

1. Windows Server Setup

Command Line:

>setx ASPNETCORE_ENVIRONMENT "Development"

SUCCESS: Specified value was saved.

or (requires admin rights)

>setx ASPNETCORE_ENVIRONMENT "Development" /M

SUCCESS: Specified value was saved.

PowerShellOrder:

$Env:ASPNETCORE_ENVIRONMENT = "Prodction"

After Windows sets the environment command, you need to re-open a command line dotnet *.dllstartup item to be effective.

2. MacOS/Linux server setup

Command Line:

export ASPNETCORE_ENVIRONMENT=development 

dotnet *.dllWhen starting the project, we can see the current one Hosting environmentto check if it is correct, example:

> dotnet AspNetCore.Samples.dll
Hosting environment: Prodtction
Content root path: C:\Users\yuezh\Desktop\Demo\AspNetCore.Samples
Now listening on: http://*:5003 Application started. Press Ctrl+C to shut down.

References:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325088509&siteId=291194637