【问题标题】:How to use options and settings in azuredeploy如何在 azuredeploy 中使用选项和设置
【发布时间】:2020-01-23 09:39:09
【问题描述】:

按照Working with options and settings 的文档Azure Function v3 和dotnet core 3.1 中的DI 可以完美运行。

但我正在寻找格式选项中的设置参数:我在 azuredeploy.json 中的 local.settgins.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"
    }
  }
},

【问题讨论】:

  • 你所说的 "...用 Visual Studio 测试部署" 是什么意思? 你说的是 Visual Studio 中的 Publish Web 部署机制?
  • 在发布之前,我从 Visual Studio 上的 ARM 项目进行部署。这是出现错误的地方。
  • 在我看来,startup.cs 中的 configuration.GetSection("option") 应该是 Values(参见 local.settings.json)。
  • 感谢您在这里分享您的解决方案,这可以使与您有相同困惑的其他人受益。可以accept your answer,让别人直接知道是工作。

标签: azure azure-devops azure-functions azure-deployment .net-core-3.1


【解决方案1】:

在文档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"
    }
  }
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2010-11-04
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多