gpt4 book ai didi

c# - 在.net core 2.1中调用时,带有连接字符串的系统环境变量返回空值

转载 作者:行者123 更新时间:2023-11-29 06:31:20 26 4
gpt4 key购买 nike

我正在尝试将连接字符串存储到我的系统(Windows 10)环境变量中,以在投入生产之前保护我的敏感信息。

当我调用环境变量时,它返回一个空值。我没有任何错误。

我确实遵循了这些教程:

https://www.youtube.com/watch?v=TNghzUs0BQI&fbclid=IwAR2YUue7740qgVT_5z04xruel-4NwOuUyjQj5E63T1UpYqRoVuz_81DiZTo

https://medium.com/@gparlakov/the-confusion-of-asp-net-configuration-with-environment-variables-c06c545ef732

我不明白我在这里缺少什么。据我了解,如果我不使用前缀,我只需要在系统中设置环境变量,然后进行调用。

这是我的环境变量的图像。我确实提供了一个链接,因为我没有足够的互联网点来发布图像。

https://i.imgur.com/j614O1n.png

这是我的 Startup.cs

        private IConfiguration _configuration;

public Startup(IConfiguration configuration)
{
_configuration = configuration;

}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddDbContext<dbApplicationServiceNavada>(options => options.UseMySql(Environment.GetEnvironmentVariable("ASNConnStr")));

services.AddScoped<IEmployeeData, SqlEmployeeData>();
services.AddScoped<ILanguageData, SqlLanguageData>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
IConfiguration configuration)
{
//Show exception stack trace when in dev mode
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

//Use files in wwwroot folder
app.UseStaticFiles();

//Defines the available routes
app.UseMvc(ConfigureRoutes);

app.Run(async (context) =>
{
var RNA = configuration["RouteNA"];
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync(RNA);
});
}

这是我在 Program.cs 中的 webHostBuilder


public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
})
.UseStartup<Startup>()
.Build();

我还看到我有一个包含环境变量的 launchSettings.json 文件,但我想知道它是否与系统环境变量相同。

第一次编辑:

正如答案中提到的,我确实将环境变量重命名为

MYSQLCONNSTR_ASNConnStr

我还删除了program.cs中加载环境变量的部分。

现在,当我调试项目时,我在 _confirguration.Providers 中看到我的环境变量已加载。这是一张图片: https://i.imgur.com/xGulUvX.png

当我这样做的时候

services.AddDbContext<dbApplicationServiceNavada>(options => options.UseMySql(_configuration.GetConnectionString("ASNConnStr")));

它仍然返回空值。我似乎无法获取该变量。

错误图片: https://i.imgur.com/Kj0mvCS.png

第二次编辑:

现在问题已经解决了,我只想向贡献者添加 Simply Ged,而不是:

services.AddDbContext<dbApplicationServiceNavada>(options => options.UseMySql(_configuration.GetConnectionString("ASNConnStr")));

我必须这样做:

services.AddDbContext<dbApplicationServiceNavada>(options => options.UseMySql(Environment.GetEnvironmentVariable("ASNConnStr")));

现在一切正常!

第三次编辑:

重新启动计算机后,这行代码可以工作:

services.AddDbContext<dbApplicationServiceNavada>(options => options.UseMySql(_configuration.GetConnectionString("ASNConnStr")));

看来我们需要重新启动计算机/服务器才能充分获取环境变量。

总之,贡献者 Simply Ged 的回答是正确的。

最佳答案

您无需添加 ConfigureAppConfiguration 即可将环境变量添加到配置中,因为它是作为 WebHost.CreateDefaultBuilder 的一部分完成的。参见 this page有关默认构建器提供的详细信息。

知道这一点后,您可以替换从 Configuration 类中读取连接字符串:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddDbContext<dbApplicationServiceNavada>(options => options.UseMySql(Configuration.GetConnectionString("ASNConnStr")));

....
}

Configuration 类将根据定义的顺序加载正确的值 here

您还需要将环境变量名称更改为以下内容:

MYSQLCONNSTR_ASNConnStr

您可以阅读有关 .NET Core 配置的更多信息 here

关于c# - 在.net core 2.1中调用时,带有连接字符串的系统环境变量返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com