在.net Core 2.0中设置环境变量
我正在尝试在我的.net核心2.0应用程序中设置多个环境,请参阅下面的代码。
配置文件(Launch.JSON)
"configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceRoot}/my.api/bin/Debug/netcoreapp2.0/my.api.dll", "args": [], "cwd": "${workspaceRoot}/my.api", "stopAtEntry": false, "requireExactSource": false, "internalConsoleOptions": "openOnSessionStart", "launchBrowser": { "enabled": true, "args": "${auto-detect-url}", "windows": { "command": "cmd.exe", "args": "/C start ${auto-detect-url}" }, "osx": { "command": "open" }, "linux": { "command": "xdg-open" } }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceRoot}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ]
Program.cs中
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); }
StartUp.cs
public class Startup { public IContainer ApplicationContainer { get; private set; } private IHostingEnvironment HostingEnvironment { get; set; } public IConfigurationRoot Configuration { get; } private string ConnectionString { get { return this.HostingEnvironment.IsDevelopment() ? Configuration.GetConnectionString("DefaultConnection") : Configuration.GetConnectionString("Production"); } } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.Azuredev.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); this.HostingEnvironment = env; System.Console.WriteLine(env.EnvironmentName); //here it always give me Production. }
我的问题
我尝试使用命令行,如dotnet run –environment“Development”
因此,它应该在开发环境上运行,但它始终使用Prodution运行,(看起来我在startup.cs中添加了console.writeline )
现在奇怪的是,如果我使用F5进行调试,那么它与开发环境完美匹配。
您可以更新launchsettings.json以包含“开发”配置文件,然后运行:
dotnet run --launch-profile "Development"
有关launchSettings.json文件配置的更多详细信息,请参阅使用多个环境
请注意,commandName可能需要是“Project”(我还没有真正尝试过这么多)。 示例launchSettings.json如下:
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:19882/", "sslPort": 0 } }, "profiles": { "Development": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
最后我做到了..
让我们看看我是如何实现这一目标的。
- 我在launchSettings.JSON中添加了所有配置文件设置
- Program.cs与我在问题中添加的相同。
- 更新了startup.cs(见下文)
- 通过终端运行它的CLI也不同。
现在先看看我的项目结构。
我的launchSettings.json中的代码
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:40088/", "sslPort": 0 } }, "profiles": { "Development": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "Azuredev": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Azuredev" } } } }
launch.json中的代码
{ "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceRoot}/my.api/bin/Debug/netcoreapp2.0/my.api.dll", "args": [], "cwd": "${workspaceRoot}/my.api", "stopAtEntry": false, "requireExactSource": false, "internalConsoleOptions": "openOnSessionStart", "launchBrowser": { "enabled": true, "args": "${auto-detect-url}", "windows": { "command": "cmd.exe", "args": "/C start ${auto-detect-url}" }, "osx": { "command": "open" }, "linux": { "command": "xdg-open" } }, "sourceFileMap": { "/Views": "${workspaceRoot}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ] }
startup.cs
public IConfigurationRoot Configuration { get; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); this.HostingEnvironment = env; }
完成所有更改后,我的API可以正常使用F5调试选项以及CLI终端。
要从命令行启动应用程序,请使用此关键字。
dotnet run –launch-profile“开发”
要么
dotnet run –launch-profile“Azuredev”
您可以在命令终端中使用以下命令设置环境。
setx ASPNETCORE_ENVIRONMENT "Environment_Name"
环境名称可以是开发,分段或生产
请注意,未在当前打开的窗口中设置环境变量。 您需要打开一个新的命令提示符以查看更新的环境。 如果打开管理命令提示符并添加/ M开关,也可以设置系统变量(而不仅仅是用户变量)
以供参考
dotnet run --environment
对ASPNETCORE_ENVIRONMENT
环境变量没有影响,请参阅此问题 。
以下是有关如何以多种方式切换环境的详细说明: https : //docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
例如,您可以从命令行运行它(在dotnet run
之前): set ASPNETCORE_ENVIRONMENT=Development
- 无法获取DbContext的reflection类型
- 升级到Core 2 Preview 2后获取“无法解析类型的服务..”
- HttpClient和ASP.NET Core 2.0 Web服务之间的连接已关闭错误
- ASP.Net Core 2配置占用了大量内存。 如何以不同方式获取配置信息?
- asp.net core 2.0中System.Web.Hosting.HostingEnvironment.RegisterObject的替代方案
- 如何为使用AutoMapper和dependency injection的.net core 2.0服务编写xUnit测试?
- 查询ASP.NET Core 2和Entity Framework Core 2中表中的附近位置
- 设置JsonConvert.DefaultSettings asp net core 2.0不能按预期工作
- 为什么AntiForgeryTokenvalidation会失败?