加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:08:03 所属栏目:百科 来源:网络整理
导读:我正在尝试在我的.net核心2.0应用程序中设置多个环境,请参阅下面的代码. Configuration file (Launch.JSON) "configurations": [ { "name": ".NET Core Launch (web)","type": "coreclr","request": "launch","preLaunchTask": "build",// If you have chang
我正在尝试在我的.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"
      }
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读