[英]How to use the app settings of an Azure App Service in a dotnet app?
Since i started working with Azure
and the App Service
resource, there has been a question that i would like to have a clear answer regarding the app settings configuration variables.自从我开始使用
Azure
和App Service
资源以来,一直有一个问题,我想对应用设置配置变量有一个明确的答案。
Looking into the documentation here , we can see the following:查看此处的文档,我们可以看到以下内容:
"In App Service, you can set app settings and connection strings outside of your app code. Then you can access them in any class using the standard ASP.NET pattern: “在应用服务中,您可以在应用代码之外设置应用设置和连接字符串。然后您可以使用标准 ASP.NET 模式在任何类中访问它们:
using System.Configuration;
...
// Get an app setting
ConfigurationManager.AppSettings["MySetting"];
// Get a connection string
ConfigurationManager.ConnectionStrings["MyConnection"];
}
If you configure an app setting with the same name in App Service and in web.config, the App Service value takes precedence over the web.config value.如果在应用服务和 web.config 中配置具有相同名称的应用设置,则应用服务值优先于 web.config 值。 The local web.config value lets you debug the app locally, but the App Service value lets your run the app in product with production settings.
本地 web.config 值允许您在本地调试应用程序,但应用程序服务值允许您在具有生产设置的产品中运行应用程序。 Connection strings work in the same way.
连接字符串的工作方式相同。 This way, you can keep your application secrets outside of your code repository and access the appropriate values without changing your code."
这样,您可以将应用程序机密保存在代码存储库之外,并在不更改代码的情况下访问适当的值。”
Does this mean, that ConfigurationManager
is the class responsible for the magic of using app service configuration variables if they exist?这是否意味着,
ConfigurationManager
是负责使用应用服务配置变量(如果存在)的魔法的类?
If we use for example如果我们使用例如
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json", true, true)
Since ConfigurationBuilder does not use, in this case, environment variables i supose the App service variables will not work anymore am i right?由于 ConfigurationBuilder 不使用,在这种情况下,环境变量我认为 App 服务变量将不再起作用,对吗?
In the example above, it's dotnet core and i looked into the documentation here and the way to configure seems to be a bit different.在上面的示例中,它是 dotnet 核心,我查看了此处的文档,配置方式似乎有点不同。 I wonder if a
.AddEnvironmentVariables();
我想知道一个
.AddEnvironmentVariables();
is required to the configuration builder.是配置生成器所必需的。
Looking into the documentation its hard to understand if its a matter of the application being configured to add environment variables or if the App Service has the capability to inject app settings in the app configuration.查看文档很难理解是应用程序被配置为添加环境变量,还是应用程序服务能够在应用程序配置中注入应用程序设置。 If we look here .
如果我们看这里。 We see this:
我们看到这个:
How will the variables with that prefix be treated?如何处理带有该前缀的变量?
For example, is it different having a variable in the configuration app settings with or without APPSETTING_
prefix?例如,在配置应用程序设置中使用或不
APPSETTING_
前缀的变量是否不同?
{
"name": "Test",
"value": "123",
"slotSetting": false
}
and和
{
"name": "APPSETTING_Test",
"value": "123",
"slotSetting": false
}
Yes, if you .AddEnvironmentVariables()
the AppSettings as configured in AppService will be included in your builder, but certain prefixes are preferable such as ASPNETCORE_
是的,如果您
.AddEnvironmentVariables()
,则 AppService 中配置的 AppSettings 将包含在您的构建器中,但某些前缀更可取,例如ASPNETCORE_
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#default-host-configuration-sources https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#default-host-configuration-sources
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.