使用IConfiguration访问Azure应用程序设置

huangapple go评论65阅读模式
英文:

Access Azure Application Settings using IConfiguration

问题

I have a setting in my Application settings on my Azure Web App set up like so:

使用IConfiguration访问Azure应用程序设置

I'm trying to access it in the code like so: configuration["AppSettings:LASTFM_API_KEY"]

This works when I have the secret set up in my secrets.json file like so:

使用IConfiguration访问Azure应用程序设置

But not when I publish my app to Azure. I first tried it in Azure as LASTFM_API_KEY and then tried adding AppSettings__, neither worked.

How can I access this secret when I publish my app? Is it possible to access it using configuration in the same way both locally and when published to Azure?

英文:

I have a setting in my Application settings on my Azure Web App set up like so:

使用IConfiguration访问Azure应用程序设置

I'm trying to access it in the code like so: configuration["AppSettings:LASTFM_API_KEY"]

This works when I have the secret set up in my secrets.json file like so:

使用IConfiguration访问Azure应用程序设置

But not when I publish my app to Azure. I first tried it in Azure as LASTFM_API_KEY and then tried adding AppSettings__, neither worked.

How can I access this secret when I publish my app? Is it possible to access it using configuration in the same way both locally and when published to Azure?

答案1

得分: 0

我错过了AddEnvironmentVariables()的调用,而且这也需要在可能用另一个文件中的类似值覆盖该值的任何调用之后进行调用(比如appsettings.json)。我把它放在最后,像这样:

IConfiguration configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddUserSecrets(Assembly.GetEntryAssembly()!, true)
    .AddEnvironmentVariables()
    .Build();
英文:

I was missing the AddEnvironmentVariables() call, and also this needs to be called after any calls that might overwrite the value with a similar value in another file (like appsettings.json). I put it last like so:

IConfiguration configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddUserSecrets(Assembly.GetEntryAssembly()!, true)
    .AddEnvironmentVariables()
    .Build();

huangapple
  • 本文由 发表于 2023年3月7日 07:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定