azure - 在哪里存储 local.settings.json 中的设置以进行 proddeployment?

标签 azure azure-functions

我对部署后应在 Azure 函数内使用的设置存储在何处感到有点困惑。

在本地测试中,我有像 local.settings.json 中的连接字符串这样的设置,建议通过以下代码读取:

[FunctionName("SomeEvent")]
public static void Run(
  [EventHubTrigger("some-hub", Connection = "EventHubConnection" )]
  EventData ev,
  ILogger log,
  ExecutionContext context,
  IConfiguration configuration,
  [EventHub("brd-iot-eventhubs", Connection = "OutputEventHubConnection")]
  ICollector<EventData> outputEvents)
  {
    var config = new ConfigurationBuilder()
   .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile(SETTINGS, optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

    string customername = config.GetValue<string>("Customer");
    string connectionString = config.GetConnectionString("myDatabase");
    [...]

现在我想在 Azure 上部署后进行设置(具有不同的值)。 AFAIK host.json 有所不同,因为这不是每个功能的配置,而是全局配置。

我可以直接在 Azure 中手动编辑属性,但我想要一个可以部署的设置文件。有什么方法可以做到这一点?也许我只需要知道设置文件的名称。

最佳答案

您可以将自定义设置放入您需要的任何文件中,并像您所做的那样使用 ConfigurationBuilder 读取它们。将自定义设置文件(例如appsettings.json)放在功能项目下,并将其设置为复制到output/publish目录(如果较新则复制),这样就可以通过VS发布进行部署。

set file property

但是对于与函数绑定(bind)相关的设置,例如 EventHubConnectionOutputEventHubConnection,函数运行时从环境变量中读取它们。我们必须回复 Azure 门户上的应用程序设置,将它们注入(inject)到环境变量中。

还要注意的是,VS 为我们提供了一个友好的 UI,以便我们在本地和 Azure 环境中配置不同的设置。我们可以选择从本地插入或者创建新的设置。(注意这里的空值表示对应的环境中没有该设置)

enter image description here

关于azure - 在哪里存储 local.settings.json 中的设置以进行 proddeployment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53759522/

相关文章:

Azure AD B2C 使用 API key 进行身份验证

azure - 无法将查询参数传递给 azure-ad-b2c 自定义策略并存储值

c# - 从 Azure Function 读取设置

python - Azure Function ImportError 在云上运行时注册函数

本地开发时的 Azure Functions 成本

Azure.Identity.AuthenticationFailedException : ManagedIdentityCredential authentication failed: Service request failed

azure - 我可以发布 Azure 辅助角色...但它不会自动启动吗?

azure - Azure AD B2C 租户和普通 Azure AD 租户有什么区别?

azure - 如何从 Azure 移动应用程序后端删除生成的查询?

json - 在 Azure Function 中动态设置计划