简体   繁体   English

如何在 dotnet 应用中使用 Azure 应用服务的应用设置?

[英]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.自从我开始使用AzureApp 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.

相关问题 是否可以在 Azure 应用服务设置中使用 JSONPath - Is it possible to use JSONPath in Azure App Service Settings Azure 应用服务应用程序设置 - Azure App Service Application settings Azure应用服务中如何指定分层应用设置? - How to specify hierarchical app settings in the Azure App Service? Azure 应用程序服务 - 数组中的应用程序设置数组 - Azure App Service - App Settings Array In Array Azure App Service升级dotnet核心 - Azure App Service upgrade dotnet core 如何在连接字符串 Azure 发布管道中使用机密 - 使用 Azure 应用服务设置任务 - How you do use Secrets in a Connection String Azure Release Pipeline - Using Azure App Service Settings Task 如何将 function 应用程序设置部署到 azure function 应用程序使用 ZCF04A02E37B774FC3597 devop8 - How to deploy function app settings to azure function app using azure devops app service deployment task 如何通过Azure App Service设置mailSettings-应用程序设置 - How to set mailSettings from Azure App Service - Application settings 部署槽设置如何在 Azure 应用服务上工作? - How do deployment slot settings work on an Azure App Service? 如何将 Azure 应用服务应用程序设置加载到 VUE 中? - How to Load Azure App Service Application Settings into VUE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM