从 App.config 在 .NET Core 控制台应用程序中设置环境变量

Geo*_*rge 11 c# asp.net-core

我们有一个 .NET Core 控制台应用程序。

static void Main(string[] args)
{
    var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    var builder = new ConfigurationBuilder()
        .AddJsonFile($"appsettings.json", true, true)
        .AddJsonFile($"appsettings.{environmentName}.json", true, true)
        .AddEnvironmentVariables();
    var configuration = builder.Build();
}
Run Code Online (Sandbox Code Playgroud)

environmentName始终为空。如何ASPNETCORE_ENVIRONMENT在App.config文件中设置?

hex*_*tor 9

OP 指定它是一个控制台应用程序。从 .NET Core 3.0 开始,对于控制台应用程序,您必须使用 DOTNET_ENVIRONMENT 而不是 ASPNETCORE_ENVIRONMENT。例如,launchsettings.json开发环境将是这样的:

"ConsoleApplication - Development": {
  "commandName": "Project",
  "environmentVariables": {
    "DOTNET_ENVIRONMENT": "Development"
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用 ASPNETCORE_ENVIRONMENT,您的控制台主机将默认为production环境。

即使控制台应用程序的 Properties 文件夹中没有 launchsettings.json 文件,只要您在控制台项目的属性窗口中更改属性,Visual Studio 就会自动创建一个 Properties 文件夹并添加一个 launchsettings.json 文件。

  • 在该文件出现之前,我必须在“调试 - 一般”下修改启动配置文件。 (2认同)

小智 0

      string value;
      bool toDelete = false;

      // Check whether the environment variable exists.
      value = Environment.GetEnvironmentVariable("Test1");
      // If necessary, create it.
      if (value == null)
      {
         Environment.SetEnvironmentVariable("Test1", "Value1");
         toDelete = true;

         // Now retrieve it.
         value = Environment.GetEnvironmentVariable("Test1");
      }
      // Display the value.
      Console.WriteLine($"Test1: {value}\n");

      // Confirm that the value can only be retrieved from the process
      // environment block if running on a Windows system.
      if (Environment.OSVersion.Platform == PlatformID.Win32NT)
      {
         Console.WriteLine("Attempting to retrieve Test1 from:");
         foreach (EnvironmentVariableTarget enumValue in
                           Enum.GetValues(typeof(EnvironmentVariableTarget))) {
            value = Environment.GetEnvironmentVariable("Test1", enumValue);
            Console.WriteLine($"   {enumValue}: {(value != null ? "found" : "not found")}");
         }
         Console.WriteLine();
      }

      // If we've created it, now delete it.
      if (toDelete) {
         Environment.SetEnvironmentVariable("Test1", null);
         // Confirm the deletion.
         if (Environment.GetEnvironmentVariable("Test1") == null)
            Console.WriteLine("Test1 has been deleted.");
      }
   }
}
// The example displays the following output if run on a Windows system:
//      Test1: Value1
//
//      Attempting to retrieve Test1 from:
//         Process: found
//         User: not found
//         Machine: not found
//
//      Test1 has been deleted.
//
// The example displays the following output if run on a Unix-based system:
//      Test1: Value1
//
//      Test1 has been deleted.
Run Code Online (Sandbox Code Playgroud)