如何在Azure Web App的ARM模板中为SQL数据库配置connectionString

时间:2019-06-13 14:54:47

标签: azure azure-web-sites arm-template

我有一个用于部署Azure Web App的ARM模板,我想使用concat函数将connectionString添加到Azure SQL中托管的现有SQL数据库中。但是当我使用模板进行部署时,我收到一条错误消息,提示无法处理模板语言表达。

我尝试了几种不同的方法来用concat函数编写连接字符串,但是它不起作用,尝试使用谷歌搜索它,但是还没有找到任何解决方案。

 ngOnInit() {
this.praticaService.getAllPratiche()
  .subscribe(res => {
    this.pratiche = res.data;
    var prova = res.data;
      this.allEntity.forEach(elem => {
      var matchedEntity = this.getNumberMatchedEntity(prova, elem);
      var totalEntity = this.getNumberEntity(prova, elem);
      var percentage = matchedEntity / totalEntity * 100 + "%";
      this.results = [{
        entity: elem,
        percentage: percentage
      }];
    })
  }, err => {
    console.log('Error: ', err);
  })
}

如果正常工作,连接字符串应采用以下格式 “服务器= tcp:.database.windows.net,1433;初始目录=;持久安全信息= False;用户ID = {your_username};密码= {your_password}; MultipleActiveResultSets = False;加密= True; TrustServerCertificate = False;连接超时= 30;

有人在输入如何在ARM模板中为connectionString编写concat吗?

2 个答案:

答案 0 :(得分:0)

您可以使用类似以下的内容。

"outputs": {
  "DbAdoConnString": {
    "type": "string",
    "value": "[concat('Server=tcp:',reference(parameters('yourservernameName')).fullyQualifiedDomainName,',1433;Initial Catalog=',parameters('dbnameName')';Persist Security Info=False;User ID=',reference(parameters('yourservernameName')).administratorLogin,';Password=',reference(parameters('yourservernameName')).administratorLoginPassword,';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;')]"
 }
}

以下是相同的输出:

dbAdoConnString String Server=tcp:yourservername.database.windows.net,1433;Initial Catalog=dbname;Persist Security Info=False;User ID=VeryWiseAdmin;Password= ReplaceWithTheMostSecurePasswordThatEverExisted&NeverShareLikeThisWithAnyone!;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

供参考,我正在添加示例模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "yourservernameName": {
      "type": "string",
      "defaultValue": "yourservername2"
    },
    "yourservernameAdminLogin": {
      "type": "string",
      "defaultValue": "VeryWiseAdmin",
      "minLength": 1
    },
    "yourservernameAdminLoginPassword": {
      "type": "securestring",
      "defaultValue": "ReplaceWithTheMostSecurePasswordThatEverExisted&NeverShareLikeThisWithAnyone!"
    },
    "dbnameName": {
      "type": "string",
      "defaultValue": "dbname",
      "minLength": 1
    },
    "dbnameCollation": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "dbnameEdition": {
      "type": "string",
      "defaultValue": "Basic"
    },
    "dbnameRequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "Basic"
    }
  },
  "variables": {
  },
  "resources": [
    {
      "name": "[parameters('yourservernameName')]",
      "type": "Microsoft.Sql/servers",
      "location": "West Europe",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [],
      "tags": {
        "displayName": "yourservername"
      },
      "properties": {
        "administratorLogin": "[parameters('yourservernameAdminLogin')]",
        "administratorLoginPassword": "[parameters('yourservernameAdminLoginPassword')]",
        "version": "12.0"
      },
      "resources": [
        {
          "name": "[concat(parameters('yourservernameName'),'/AllowAllWindowsAzureIps')]",
          "type": "Microsoft.Sql/servers/firewallRules",
          "location": "[resourceGroup().location]",
          "apiVersion": "2014-04-01-preview",
          "dependsOn": [
            "[resourceId('Microsoft.Sql/servers', parameters('yourservernameName'))]"
          ],
          "properties": {
            "startIpAddress": "0.0.0.0",
            "endIpAddress": "0.0.0.0"
          }
        },
        {
          "name": "[concat(parameters('yourservernameName'),'/',parameters('dbnameName'))]",
          "type": "Microsoft.Sql/servers/databases",
          "location": "West Europe",
          "apiVersion": "2014-04-01-preview",
          "dependsOn": [
            "[resourceId('Microsoft.Sql/servers', parameters('yourservernameName'))]"
          ],
          "tags": {
            "displayName": "dbname"
          },
          "properties": {
            "collation": "[parameters('dbnameCollation')]",
            "edition": "[parameters('dbnameEdition')]",
            "maxSizeBytes": "1073741824",
            "requestedServiceObjectiveName": "[parameters('dbnameRequestedServiceObjectiveName')]"
          }
        }
      ]
    }
  ],
  "outputs": {
    "SomeString": {
      "type": "string",
      "value": "What ever you want to put here"
    },
    "ServerNameParam": {
      "type": "string",
      "value": "[parameters('yourservernameName')]"
    },
    "ServerResourceID": {
      "type": "string",
      "value": "[resourceId('Microsoft.Sql/servers', parameters('yourservernameName'))]"
    },
    "ServerObject": {
      "type": "object",
      "value": "[reference(parameters('yourservernameName'))]"
    },
    "SqlServerURL": {
      "type": "string",
      "value": "[reference(parameters('yourservernameName')).fullyQualifiedDomainName]"
    },
    "DbResourceID": {
      "type": "string",
      "value": "[resourceId('Microsoft.Sql/servers/databases', parameters('yourservernameName'), parameters('dbnameName'))]"
    },
    "DbObject": {
      "type": "object",
      "value": "[reference(parameters('dbnameName'))]"
    },
    "DbAdoConnString": {
      "type": "string",
      "value": "[concat('Server=tcp:',reference(parameters('yourservernameName')).fullyQualifiedDomainName,',1433;Initial Catalog=',parameters('dbnameName'),';Persist Security Info=False;User ID=',reference(parameters('yourservernameName')).administratorLogin,';Password=',reference(parameters('yourservernameName')).administratorLoginPassword,';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;')]"
    }
  }

}

看看是否有帮助。

答案 1 :(得分:0)

您可以通过将以下子资源添加到您的应用程序服务资源中来向应用程序服务添加连接字符串:

{
          "name": "connectionstrings",
          "type": "config",
          "apiVersion": "2018-11-01",
          "dependsOn": [
            "[resourceId('Microsoft.Web/sites', variables('MyAppService'))]",
            "[resourceId('Microsoft.Sql/servers/databases', variables("MySqlServerName"), variables('MyDatabaseName'))]"
          ],
          "tags": {
            "displayName": "Connection strings"
          },
          "properties": {
            "MyConnectionStringName": {
              "value": "[concat('Data Source=',reference(resourceId('Microsoft.Sql/servers', variables('MySqlServerName'))).fullyQualifiedDomainName,';Initial Catalog=',variables('MyDatabaseName'),';UID=someUser;pwd=somePassword')]",
              "type": "SQLServer"
            }
          }
}

此资源取决于已创建的应用程序服务和数据库。然后,它使用数据库服务器的标准名称将连接字符串填充到应用程序服务中。