在.net Core 2.0中设置环境变量

Bha*_*rat 12 c# asp.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}"
    }
]
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

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.
    }
Run Code Online (Sandbox Code Playgroud)

我的问题

我尝试使用命令行,如dotnet run --environment"Development"

因此,它应该在开发环境上运行,但它始终使用Prodution运行,(看看我在startup.cs中添加了console.writeline)

现在奇怪的是,如果我使用F5进行调试,那么它与开发环境完美匹配.

Cal*_*alC 13

您可以更新launchsettings.json以包含"开发"配置文件,然后运行:

dotnet run --launch-profile "Development"
Run Code Online (Sandbox Code Playgroud)

有关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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Bha*_*rat 6

终于我做到了。

让我们看看我是如何实现的。

  1. 我已经在launchSettings.JSON中添加了所有配置文件设置
  2. Program.cs仍然与我在问题中添加的相同。
  3. 更新了startup.cs(请参见下文)
  4. 通过终端运行它的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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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;        
    }
Run Code Online (Sandbox Code Playgroud)

完成所有这些更改之后,我的API可以与F5调试选项以及CLI终端一起正常工作。

要从命令行启动应用程序,请使用此关键字。

dotnet运行--launch-profile“开发”

要么

dotnet运行--launch-profile“ Azuredev”

  • 伟大的调试?如何为开发环境变量值设置默认调试? (2认同)