在.NET Core 2.0中设置环境变量 [英] Setting environment variables in .NET Core 2.0

查看:125
本文介绍了在.NET Core 2.0中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在.NET Core 2.0应用程序中设置多个环境.请参阅下面的代码.

I am trying to setting up multiple environments in my .NET Core 2.0 application. See my code below.

"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 run --environment"Development"

因此,它应该在开发环境上运行,但始终与生产一起运行,(看起来我已经在我的 console.writeline 中添加了它) startup.cs 文件)

So, it should run on Development Environment, but it always runs with Production, (look I have added console.writeline in my startup.cs file)

现在奇怪的是,如果我使用 F5 进行调试,那么它可以在 development 环境中完美运行.

Now the strange thing is that if I use F5 to Debug then it runs perfectly with the development environment.

推荐答案

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

You can update your launchsettings.json to include a 'Development' profile and then run:

dotnet run --launch-profile "Development"

有关launchSettings.json文件配置的更多详细信息,请参见使用多种环境

For further details on configuration of the launchSettings.json file see Working with multiple environments

请注意,commandName可能需要为"Project"(我并没有做太多尝试).示例launchSettings.json如下:

Note that the commandName would probably need to be "Project" (I haven't really tried this much). Example launchSettings.json as follows:

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

这篇关于在.NET Core 2.0中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆