【问题标题】:Azure Functions settings not consistent betwen portal and localAzure Functions 设置在门户和本地之间不一致
【发布时间】:2019-06-21 21:42:00
【问题描述】:

我正在使用 Azure 函数,但遇到了问题。 我用我的变量声明了一个local.settings.json 文件,如下所示:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "TopicEndpoint": "my endpoint"
  }  
}

这允许我的天蓝色函数使用以下方法读取设置:

var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
var myTopic = config["Values:TopicEndpoint"];

这允许我通过以下方式发布变量并将其导出到门户:

func azure functionapp publish myfunctionapp --publish-local-settings -i

但是,在发布并验证该值在门户的“应用程序设置”中时,“Values:TopicEndpoint”不存在。

为了能够访问它的值,我必须将我的变量直接放在 json 根目录下:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  }  
  "TopicEndpoint": "my endpoint"
}

这样我就可以在本地开发环境以及 Azure 上安全地使用 config['TopicEndpoint']。但是,这违背了--publish-local-settings -i 的目的,因为它只导出在“值”键下找到的值,所以我必须手动创建所有设置。

你知道为什么会发生这种情况,或者我是否遗漏了什么?

【问题讨论】:

  • 部署时是否显示App setting AzureWebJobsStorage is different between azure and local.settings.json Would you like to overwrite value in azure? [yes/no/show]
  • 不,它在部署期间没有显示警告,但我肯定门户上的值在 Values 键中声明时得到了更新。
  • 也许你可以试试--overwrite-settings [-y]--publish-local-settings -i。你是什​​么意思门户上的值在 Values 键中声明时得到更新?你之前说过Values:TopicEndpoint不存在。
  • 确定不存在。它作为应用程序设置更新,只能通过执行 config["TopicEndpoint"] 读取,并且不在“Values”键内

标签: azure azure-functions


【解决方案1】:

我猜你应该以不同的方式使用它。问题是来自local.settings.jsoncome as environment variables 的值会自动生成,因此您根本不必担心使用该文件。这就是我们在项目中的做法,它适用于所有环境。

// see, just env variables
var configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();

// and then use
var value = configuration["TopicEndpoint"];

【讨论】:

  • 我查看了文档并同意您的方法。但是我已经在“值”中添加了我的键,但我没有看到它们作为环境变量添加,这可能是一个已知问题吗?
  • 这根本不是问题,我们在项目中就是这样做的。您是否尝试过我发布的代码?
【解决方案2】:

我发现了问题。 问题是我按照这里的建议添加了一个单独的 secret.settings.json https://www.tomfaltesek.com/azure-functions-local-settings-json-and-source-control/ 因此我像这样加载我的配置:

var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

我的本​​地设置文件如下所示:

{
  "Values": {
      "TopicEndpoint": "my endpoint"
  }  
}

我的秘密设置是这样的:

{
  "Values": {
      "TopicKey": "my key"
  }  
}

这引起了冲突,所以我需要从 secret.settings.json 中删除“值”键,使其看起来像这样:

{
  "TopicKey": "my key"
}

这样,我既可以使用func azure functionapp publish myfunctionapp --publish-local-settings -i 来部署本地设置文件中的值,也可以使用这两个文件作为环境变量。

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多