简体   繁体   English

Azure Functions v2 - 新的 .netcore 配置和部署

[英]Azure Functions v2 - New .netcore Configuration and deployment

Now that we can use the hugely flexible configuration engine from .NETCore - we can do something like this :现在我们可以使用 .NETCore 非常灵活的配置引擎 - 我们可以这样做:

   private static IConfigurationRoot SetConfig(ExecutionContext executionContext)
    {
        return new ConfigurationBuilder()
           .SetBasePath(executionContext.FunctionAppDirectory)
           .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables()
           .Build();
    }

Which is great as it allow you to put more complicated configuration data in the config file - for instance这很棒,因为它允许您将更复杂的配置数据放入配置文件中 - 例如

    {
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<< removed >>",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "<< removed >>"
  },
  "MyCustomSettings": [
    {
      "ConnectionString": "<< removed >>",
      "Folders": [
        {
          "ShareName": "share1",
          "FolderName": "folder1"
        },
        {
          "ShareName": "share2",
          "FolderName": "folder2"
        }
      ]
    }
  ]
}

Again - great news!再次 - 好消息! I can now get access to my strongly typed configuration with config["MyCustomSettings"]我现在可以使用 config["MyCustomSettings"] 访问我的强类型配置

What I don't get though - is how this can be deployed when publishing the function.我不明白的是 - 发布功能时如何部署它。 Only the Values section is migrated to the Azure function Application Settings.只有值部分迁移到 Azure 函数应用程序设置。 I can obviously put this custom json in a json file and add it to the load statement like the local.settings.json我显然可以将这个自定义 json 放在一个 json 文件中,并将其添加到 load 语句中,如 local.settings.json

.AddJsonFile("my-custom-settings.json", optional: false, reloadOnChange: true)

but then this file has to be included in the deploy, and is not stored securely.但是这个文件必须包含在部署中,并且没有安全存储。

Any ideas?有任何想法吗?

This is not officially supported as of Nov 2019.截至 2019 年 11 月,这尚未得到官方支持。

Warning 警告

Avoid attempting to read values from files like local.settings.json or appsettings.{environment}.json on the Consumption plan.避免尝试从消耗计划中的 local.settings.json 或 appsettings.{environment}.json 等文件中读取值。 Values read from these files related to trigger connections aren't available as the app scales because the hosting infrastructure has no access to the configuration information.从这些文件中读取的与触发器连接相关的值在应用扩展时不可用,因为托管基础结构无法访问配置信息。

There are so many blogs around advising to do this and it appears this may work if you only have a single instance, but as soon as the ScaleController triggers scaling, new instances will not be able to find the config files. 有很多博客都建议这样做,如果您只有一个实例,这似乎可行,但是一旦 ScaleController 触发缩放,新实例将无法找到配置文件。

If you have triggers that use the %SettingName% syntax, they will not work as the function scales.如果您有使用%SettingName%语法的触发器,它们将不会在函数缩放时起作用。

Functions team is considering possible Options (lol)职能团队正在考虑可能的选项(笑)

There is also the option of using the new App Configuration service but it is currently in preview and isn't available in all Azure regions.还可以选择使用新的App Configuration服务,但它目前处于预览状态,并非在所有 Azure 区域都可用。

It may be simpler to put your config in blob storage, and load it at startup? 将您的配置放在 blob 存储中并在启动时加载它可能更简单? (Your blob store connectionstring will need to be in env variables) (您的 blob 存储连接字符串需要位于 env 变量中)

So far the best we can do is to include your "not so secret" settings (things like MyThingTimeout or ExternalEndpointAddress ) in a json file and use .AddJsonFile(...) and put secrets in KeyVault.到目前为止,我们能做的最好的事情是将您的“不那么秘密”的设置(比如MyThingTimeoutExternalEndpointAddress )包含在一个 json 文件中,并使用.AddJsonFile(...)并将秘密放在 KeyVault 中。 This does force you to split your config and decide which goes where.这确实会迫使您拆分配置并决定哪个去哪里。 (And also make sure your triggers only read from the Values section/Environment Variables) (并确保您的触发器仅从“值”部分/环境变量中读取)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM