2017-09-27 75 views
1

我想在我的.net核心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<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运行--environment “发展”

所以,它应该永远在开发环境但它运行运行生产,(看我已经在我的startup.cs中添加了console.writeline

现在奇怪的是,如果我使用F5来调试,那么它运行完美发展环境。

回答

4

您可以更新launchsettings。 JSON包含一个“发展”配置文件,然后运行:

dotnet run --launch-profile "Development" 

进一步的细节上launchSettings.json文件的配置看Working with multiple environments

请注意,commandName可能需要是“项目”(我没有真正尝试过这么多)。例如launchSettings.json如下:

{ 
    "iisSettings": { 
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": { 
     "applicationUrl": "http://localhost:19882/", 
     "sslPort": 0 
    } 
    }, 
    "profiles": { 
    "Development": { 
     "commandName": "Project", 
     "launchBrowser": true, 
     "environmentVariables": { 
     "ASPNETCORE_ENVIRONMENT": "Development" 
     } 
    } 
    } 
} 
+0

是在launch.json env标签下的Profile标签? – Bharat

+0

@Bharat - 它在launchSettings.json中不是launch.json(属性文件夹中) – CalC

+0

ok,所以我必须创建一个并行launchSettings文件。 – Bharat

0

最后我做到了..

让我们看一下我是如何实现这一点。

  1. 我在launchSettings.JSON加了我所有的个人资料设置,我在我的问题添加
  2. Program.cs中保持不变。
  3. 更新的启动。cs(见下文)
  4. CLI通过终端运行它也不同。

现在先看看我的项目结构。

enter image description here

在我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运行--launch瞩目的 “发展”

OR

DOTNET运行--launch瞩目的 “Azuredev”