【发布时间】:2018-03-08 18:30:11
【问题描述】:
我正在尝试在我的 .NET Core 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}"
}
]
程序.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.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"
之类的命令行所以,它应该在 Development Environment 上运行,但它总是在 Production 下运行,(看我在我的startup.cs 文件)
现在奇怪的是,如果我使用 F5 进行调试,那么它可以在 development 环境中完美运行。
【问题讨论】:
-
我创建了一个演示 github.com/d668/NetCoreConfigTransform .NET Core 2.2 控制台应用 App.config 使用 slow-cheetah 进行转换
标签: c# asp.net-core-2.0