arrays - 将参数文件中的连接字符串数组传递到 bicep 文件,以用于应用程序服务的循环

标签 arrays json azure for-loop azure-bicep

我正在尝试创建一个应用程序服务以通过 CI/CD 管道进行部署,但是我正在努力为连接字符串创建一个 For 循环。

我的参数文件有以下内容:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        // Required Parameters
        ...
        // Optional Parameters
        "connectionStrings": [
            {
                "name": "Tracking",
                "connectionString": "...",
                "type": "SQLServer"
            }
            {
                "name": "Logs",
                "connectionString": "...",
                "type": "SQLServer"
            }
        ],
        ...
    }
}

我将其作为 param connectionStrings 数组 传递到 main.bicep 文件和 appService.bicep 模块文件中。

appService.bicep 文件中,我有以下 for 循环。

resource appServiceWeb 'Microsoft.Web/sites@2022-03-01' = {
  name: nameWeb
  location: location
  tags: tags
  kind: kind
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: appServicePlan_id
    siteConfig: {
      appSettings: [
        ...
      ]
      connectionStrings: [for connectionString in connectionStrings: {
        name: connectionString.name
        connectionString: connectionString.connectionString
        type: connectionString.type
      }]
    }
  }
}

我收到以下错误:

{
  "code": "InvalidRequestContent",
  "message": "The request content was invalid and could not be deserialized: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Azure.Deployments.Core.Definitions.DeploymentParameterDefinition' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath 'properties.parameters.connectionStrings', line 1, position 280.'."
}

最佳答案

我已经成功解决了我的问题。虽然我的 Json 结构是合法的,但它不是二头肌文件的正确结构。

我需要添加下面的value部分:

"connectionStrings": {
  "value": [
    {
      "name": "TrackingEntities",
      "connectionString": "...",
      "type": "SQLServer"
    }
  ]
},

关于arrays - 将参数文件中的连接字符串数组传递到 bicep 文件,以用于应用程序服务的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75458578/

相关文章:

azure - 是否可以在 azcopy 中使用正则表达式?

c# - 通过 Azure SignalR 连接 .net6 Web 服务和 .net6 wpf 应用程序

php - 按单维数组对多维数组进行排序

javascript - 匹配数组和返回值

javascript - 如何测试数组项的文本是否包含子字符串?

php - 对PHP多维数组的格式感到困惑

php - json_decode 帮助

javascript - 正在渲染对象数组的数组,不断获取 "Warning: Each child in a list should have a unique "键“prop”。

javascript - 我可以在 .json 对象中传递要与 css 一起使用的类吗?

azure - 将本地文件复制到 Azure Blob - 长文件名