如何读取 dotnet 核心上的 Windows 环境变量?
How to read Windows environment variables on dotnet core?
我正在使用 dotnet core 3.1 构建 Web API,现在我需要在 Windows 环境变量上存储一种敏感信息,我已经这样做了。我们称它为 MY_WIN_VAL_X。
我尝试在 Startup.cs (link)
的 ConfigureServices 方法上使用以下代码
public class Startup
{
private IHostEnvironment Environment { get; set; }
public Startup(IConfiguration configuration, IHostEnvironment environment)
{
Environment = environment;
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...other code
var myEnvValX = Environment.GetEnvironmentVariable("MY_WIN_VAL_X");
}
}
但是,我得到了这个错误:
'IHostEnvironment' does not contain a definition for
'GetEnvironmentVariable' and no accessible extension method
'GetEnvironmentVariable' accepting a first argument of type
'IHostEnvironment' could be found (are you missing a using directive
or an assembly reference?) [API]csharp(CS1061)
有人知道怎么实现吗?如何获取自定义 Windows 环境变量?
读取环境变量是通过 Configuration mechanism 完成的,因此可以通过 IConfiguration
对象而不是 IHostEnvironment
对象访问。
要将环境变量添加到您的配置中,您需要将其添加到您的 Program.cs
文件中:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables(); // <-- this line
// optional: use a prefix
//config.AddEnvironmentVariables(prefix: "MYPREFIX_"); // <-- this line
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
然后你可以像这样读取你的环境变量:
public class Startup
{
private IConfiguration Configuration { get; set; }
private IHostEnvironment Environment { get; set; }
public Startup(IConfiguration configuration, IHostEnvironment environment)
{
Environment = environment;
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...other code
var myEnvValX = this.Configuration["MY_WIN_VAL_X"];
}
}
请注意,您可以通过添加前缀来限制哪些环境变量可用。例如,如果你有 MYPREFIX_MY_WIN_VAL_X
、MYPREFIX_MY_WIN_VAL_Y
,你可以用 config.AddEnvironmentVariables(prefix: "MYPREFIX_")
限制 env 变量,只有这两个变量会被暴露(前缀会被删除)。
我正在使用 dotnet core 3.1 构建 Web API,现在我需要在 Windows 环境变量上存储一种敏感信息,我已经这样做了。我们称它为 MY_WIN_VAL_X。
我尝试在 Startup.cs (link)
的 ConfigureServices 方法上使用以下代码public class Startup
{
private IHostEnvironment Environment { get; set; }
public Startup(IConfiguration configuration, IHostEnvironment environment)
{
Environment = environment;
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...other code
var myEnvValX = Environment.GetEnvironmentVariable("MY_WIN_VAL_X");
}
}
但是,我得到了这个错误:
'IHostEnvironment' does not contain a definition for 'GetEnvironmentVariable' and no accessible extension method 'GetEnvironmentVariable' accepting a first argument of type 'IHostEnvironment' could be found (are you missing a using directive or an assembly reference?) [API]csharp(CS1061)
有人知道怎么实现吗?如何获取自定义 Windows 环境变量?
读取环境变量是通过 Configuration mechanism 完成的,因此可以通过 IConfiguration
对象而不是 IHostEnvironment
对象访问。
要将环境变量添加到您的配置中,您需要将其添加到您的 Program.cs
文件中:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables(); // <-- this line
// optional: use a prefix
//config.AddEnvironmentVariables(prefix: "MYPREFIX_"); // <-- this line
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
然后你可以像这样读取你的环境变量:
public class Startup
{
private IConfiguration Configuration { get; set; }
private IHostEnvironment Environment { get; set; }
public Startup(IConfiguration configuration, IHostEnvironment environment)
{
Environment = environment;
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...other code
var myEnvValX = this.Configuration["MY_WIN_VAL_X"];
}
}
请注意,您可以通过添加前缀来限制哪些环境变量可用。例如,如果你有 MYPREFIX_MY_WIN_VAL_X
、MYPREFIX_MY_WIN_VAL_Y
,你可以用 config.AddEnvironmentVariables(prefix: "MYPREFIX_")
限制 env 变量,只有这两个变量会被暴露(前缀会被删除)。