ARM模板 - web.config转换无法正常工作

时间:2017-02-08 11:08:11

标签: asp.net-mvc azure azure-web-sites azure-pipelines-release-pipeline arm-template

我正在尝试使用Arm模板部署网站和sql azure,我正在尝试使用部署的数据库名称使用部署的网站转换sql连接字符串。创建了网站,部署了源代码并创建了sql azure数据库,但连接字符串不会被更改。 我在这里遵循了这个方法: https://github.com/Azure/azure-quickstart-templates/tree/master/201-web-app-sql-database

我的website.json看起来像这样:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "hostingPlanName": {
      "type": "string",
      "minLength": 1
    },
    "environmentName": {
      "type": "string",
      "allowedValues": [
        "integration",
        "qa"
      ]
    },
    "skuName": {
      "type": "string",
      "defaultValue": "F1",
      "allowedValues": [
        "F1",
        "D1",
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P4"
      ],
      "metadata": {
        "description": "Describes plan's pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
      }
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "metadata": {
        "description": "Describes plan's instance count"
      }
    },
    "sqlserverAdminLogin": {
      "type": "string",
      "minLength": 1
    },
    "sqlserverAdminLoginPassword": {
      "type": "securestring"
    },
    "databaseName": {
      "type": "string",
      "minLength": 1
    },
    "databaseCollation": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "databaseEdition": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ]
    },
    "databaseRequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "S0",
        "S1",
        "S2",
        "P1",
        "P2",
        "P3"
      ],
      "metadata": {
        "description": "Describes the performance level for Edition"
      }
    }
  },
  "variables": {
    "webSiteName": "[concat('webSite-MyApp-', uniqueString(resourceGroup().id))]",
    "sqlserverName": "[concat('sqlserver-', parameters('environmentName'), '-',  uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[parameters('hostingPlanName')]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "HostingPlan"
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "capacity": "[parameters('skuCapacity')]"
      },
      "properties": {
        "name": "[parameters('hostingPlanName')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
      ],
      "location": "[resourceGroup().location]",
      "name": "[variables('webSiteName')]",
      "properties": {
        "name": "[variables('webSiteName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
      },
      "resources": [
        {
          "apiVersion": "2016-03-01",
          "type": "config",
          "name": "connectionstrings",
          "dependsOn": [
            "[variables('webSiteName')]"
          ],
          "properties": {
            "DefaultConnection": {
              "value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('databaseName'), ';User Id=', parameters('sqlserverAdminLogin'), '@', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ';Password=', parameters('sqlserverAdminLoginPassword'), ';')]",
              "type": "SQLAzure"
            }
          }
        }
      ],
      "tags": {
        "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
        "displayName": "Website"
      },
      "type": "Microsoft.Web/sites"
    },
    {
      "name": "[variables('sqlserverName')]",
      "type": "Microsoft.Sql/servers",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "SqlServer"
      },
      "apiVersion": "2014-04-01",
      "properties": {
        "administratorLogin": "[parameters('sqlserverAdminLogin')]",
        "administratorLoginPassword": "[parameters('sqlserverAdminLoginPassword')]",
        "version": "12.0"
      },
      "resources": [
        {
          "name": "[parameters('databaseName')]",
          "type": "databases",
          "location": "[resourceGroup().location]",
          "tags": {
            "displayName": "Database"
          },
          "apiVersion": "2015-01-01",
          "dependsOn": [
            "[variables('sqlserverName')]"
          ],
          "properties": {
            "edition": "Basic",
            "collation": "SQL_Latin1_General_CP1_CI_AS",
            "maxSizeBytes": "1073741824",
            "requestedServiceObjectiveName": "Basic"
          }
        },
        {
          "type": "firewallrules",
          "apiVersion": "2014-04-01",
          "dependsOn": [
            "[variables('sqlserverName')]"
          ],
          "location": "[resourceGroup().location]",
          "name": "AllowAllWindowsAzureIps",
          "properties": {
            "endIpAddress": "0.0.0.0",
            "startIpAddress": "0.0.0.0"
          }
        }
      ]
    }
  ],
  "outputs": {
    "siteUri": {
      "type": "string",
      "value": "[reference(concat('Microsoft.Web/sites/', variables('webSiteName'))).hostnames[0]]"
    },
    "sqlServerFullyQualifiedDomain": {
      "type": "string",
      "value": "[reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName]"
    }
  }
}

我的参数文件如下:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "hostingPlanName": {
      "value": "MyAppIntegration"
    },
    "environmentName": {
      "value": "integration"
    },
    "sqlserverAdminLogin": {
      "value": "azureuser"
    },
    "sqlserverAdminLoginPassword": {
      "reference": {
        "keyVault": {
          "id": "/subscriptions/XXXXXXXX/resourceGroups/resourceGroupName/providers/Microsoft.KeyVault/vaults/MyAppVault"
        },
        "secretName": "SqlAzurePassword"
      }
    },
    "databaseName": {
      "value": "myapp-integration" 
    }
  }
}

我的web.config看起来像这样:

ml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MyApp-20170201054732.mdf;Initial Catalog=aspnet-MyApp-20170201054732;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

有人能告诉我为什么DefaultConnection永远不会改变吗?

2 个答案:

答案 0 :(得分:1)

我刚刚部署了它,可以确认应用程序设置是否正确填充。 Web.config不应被此模板覆盖,模板会创建应用程序设置,应用程序将其读取:

如果您的web.config文件中已经存在应用程序设置,则Windows Azure网站将在运行时使用与您的网站关联的值自动覆盖它们。连接字符串以类似的方式工作,只需要很少的额外要求。请记住,之前有一个名为“example-config_db”的连接字符串已与网站关联。如果网站的web.config文件在配置部分中引用相同的连接字符串,则Windows Azure网站将使用门户中显示的值在运行时自动更新连接字符串。

参考:https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/

答案 1 :(得分:0)

DefaultConnection中的web.config连接字符串无法通过ARM部署进行更新,因为您实际上需要将web.config文件部署为MSDeploy包的一部分,并带有用于更新的参数DefaultConnection连接字符串,使用ARM模板中的MSDeploy扩展资源。

E.g。在MSDeploy Package parameters.xml中,您需要定义一个参数以允许更新名为DefaultConnection的连接字符串的值。

  <parameter name="Default Connection String" description="Connection string to enter into config" tags="SQL, Hidden,NoStore">
    <parameterEntry kind="XmlFile" scope="Web\.config$" match="//connectionStrings/add[@name='DefaultConnection']/@connectionString" />
  </parameter>

除上述内容外,您还需要添加 MSDeploy扩展资源,该资源是您网站资源下的子资源,并设置默认连接字符串参数使用ARM模板中的Azure SQL连接字符串。

{
  "apiVersion": "2015-08-01",
  "dependsOn": [
    "[resourceId('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
  ],
  "location": "[resourceGroup().location]",
  "name": "[variables('webSiteName')]",
  "properties": {
    "name": "[variables('webSiteName')]",
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
  },
"resources": [
    {
        "name": "MSDeploy",
        "type": "extensions",
        "location": "[resourceGroup().location]",
        "apiVersion": "2016-08-01",
        "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"
        ],
        "tags": {
            "displayName": "webDeploy"
        },
        "properties": {
            "packageUri": "[parameters('MSDeployPackageUri')]", 
            "dbType": "None",
            "connectionString": "",
            "setParameters": {
                "Application Path": "[variables('webSiteName')]",
                "Default Connection String": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('databaseName'), ';User Id=', parameters('sqlserverAdminLogin'), '@', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ';Password=', parameters('sqlserverAdminLoginPassword'), ';')]"
            }
        }
    }
],
  "tags": {
    "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
    "displayName": "Website"
  },
  "type": "Microsoft.Web/sites"
},...

参考:Deploy a web app with MSDeploy, custom hostname and SSL certificate