2017-09-27 76 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」