在Bicep中传入连接字符串数组配置App Service循环时报错
Azure应用服务CI/CD部署:连接字符串数组参数反序列化错误解决办法
问题场景
通过CI/CD管道部署Azure应用服务时,尝试用Bicep循环配置连接字符串,参数文件中定义了connectionStrings数组,但部署时收到反序列化错误,提示无法将JSON数组转为DeploymentParameterDefinition类型。
参数文件代码:
{ "$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" } ], ... } }
Bicep模块(appService.bicep)中的循环代码:
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.'." }
错误原因
Azure部署参数文件的规范要求:所有类型的参数都必须通过value字段传递值,包括数组类型。当前参数文件中connectionStrings直接以数组形式作为参数值,不符合规范,导致Azure无法正确解析参数类型。
修复方案
1. 修改参数文件格式
将connectionStrings数组包裹在value对象中,修正后的参数文件如下:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { // Required Parameters ... // Optional Parameters "connectionStrings": { "value": [ { "name": "Tracking", "connectionString": "...", "type": "SQLServer" }, { "name": "Logs", "connectionString": "...", "type": "SQLServer" } ] }, ... } }
2. 保留Bicep代码逻辑
main.bicep和appService.bicep中的参数定义(param connectionStrings array)以及循环配置连接字符串的代码无需修改,保持原样即可。
验证
修改参数文件后重新触发CI/CD部署,即可正常解析数组参数,连接字符串会被正确批量配置到Azure应用服务中。
内容的提问来源于stack exchange,提问作者Ross
相关产品推荐
相关产品推荐

