如何在 azuredeploy 中使用选项和设置

How to use options and settings in azuredeploy

Azure Function v3dotnet core 3.1 中使用 DI 遵循文档 Working with options and settings 完美工作。

但我正在搜索格式为 option:setting 的设置参数,我在 local.settgins.json 中有 azuredeploy.json

在 Visual Studio 中测试部署 ARM 项目时出现此错误。 我还没有尝试,但我也需要在 Azure DevOps 管道变量中为不同的环境设置一个参数。

非常感谢大家的帮助。

错误

failed with message '{
  "Code": "BadRequest",
  "Message": "AppSetting with name 'option:setting' is not allowed.",
  "Target": null,
  "Details": [
    {
      "Message": "AppSetting with name 'option:setting' is not allowed."
    },
    {
      "Code": "BadRequest"
    },
    {
      "ErrorEntity": {
        "ExtendedCode": "04072",
        "MessageTemplate": "AppSetting with name '{0}' is not allowed.",
        "Parameters": [
          "option:setting"
        ],
        "Code": "BadRequest",
        "Message": "AppSetting with name 'option:setting' is not allowed."
      }
    }
  ],
  "Innererror": null
}'

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "option:setting": "value"
  }
}

Startup.cs

[assembly: FunctionsStartup(typeof(MyProject.Startup))]
namespace MyProject
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddOptions<MyOption>()
                .Configure<IConfiguration>((settings, configuration) => configuration.GetSection("option").Bind(settings));
        }
    }
}

azuredeploy.parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    ...
    "option": {
      "value": {
        "setting": "value"
      }
    }
  }
}

azuredeploy.json

"resources": [
{
  "apiVersion": "2017-05-10",
  "name": "functionApp",
  "type": "Microsoft.Resources/deployments",
  "properties": {
    "parameters": {
      ...
      "additionalAppSettings": {
        "value": {
          "MSDEPLOY_RENAME_LOCKED_FILES": "1",
          "option:setting": "[parameters('option').setting]"
        }
      }
    },
    "debugSetting": {
      "detailLevel": "requestContent,responseContent"
    }
  }
},

在文档中找到它Configure an App Service app in the Azure portal

只需编辑您的 local.settings.json 并将 char : 替换为 __(有 2 个字符)。

GetSection 方法可以正常工作。

现在对 azuredeploy.json 做同样的事情。

瞧,部署工作完美,Cerise sur le gateau,它也适用于 Linux。

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "option__setting": "value"
  }
}

azuredeploy.json

"resources": [
{
  "apiVersion": "2017-05-10",
  "name": "functionApp",
  "type": "Microsoft.Resources/deployments",
  "properties": {
    "parameters": {
      ...
      "additionalAppSettings": {
        "value": {
          "MSDEPLOY_RENAME_LOCKED_FILES": "1",
          "option__setting": "[parameters('option').setting]"
        }
      }
    },
    "debugSetting": {
      "detailLevel": "requestContent,responseContent"
    }
  }
},