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

前端之家收集整理的这篇文章主要介绍了c# – 在.net Core 2.0中设置环境变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的.net核心2.0应用程序中设置多个环境,请参阅下面的代码.

Configuration file (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","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",reloadOnChange: true)
            .AddJsonFile($"appsettings.Azuredev.json",reloadOnChange: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();

        this.HostingEnvironment = env;         

        System.Console.WriteLine(env.EnvironmentName); //here it always give me Production.
    }

My issue

我尝试使用命令行,如dotnet run –environment“Development”

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

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

解决方法

您可以更新launchsettings.json以包含“开发”配置文件,然后运行:
dotnet run --launch-profile "Development"

有关launchSettings.json文件配置的更多详细信息,请参阅Working with multiple environments

请注意,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"
      }
    }
  }
}

猜你在找的.NET Core相关文章