Freigeben über


Auto-Parameter Connection String for Web Deploy Package

Scenario:

The connection string from the Visual Studio's project web.config over rides the connection string set in the web deploy package parameters file upon deployment of the package. Meaning, the connection string defined in the in the web.config is connectionString="Server=ServerA and web deploy package has a parameter value of connectionString="Server=ServerB and the end result of the deployment is connectionString="Server=ServerA appears in the web.config. The ServerA is used for testing by the development team and ServerB is the production server and is the expected value as it is setup in the deployment process for the environment.

It appears the Web Deploy process auto-adds the connection strings to the deployment package and additional steps are required to prevent this from happening during the deployment as it can lead to two connection strings with the same name in the web.config file and also for the production web applications to point to the dev servers.

The resolutions are described in this StackOverflow post and the easiest method to resolve the issue is to change the name of the connection string variable in the Parameters.xml file from name="SQLServerConn" (Original value) to name="SQLServerConn-Web.config Connection String"

This does not require an update to the Visual Studio project file and is the easiest to implement. You can stop reading this blog post if this makes sense. If not, please continue and I will try to explain in as much detail as possible.

The process described below uses the following deployment methodology:

  • Web Deploy package is created for deployment and parameters are defined using the Parameters.xml file in the Visual Studio project
  • Release Manager uses the tokenization process to replace values in the parameters.xml file with the variables to use for each environment
  • Batch file is run on the local IIS server using the Web Deploy command to update the website and uses the values in the SetParameters.xml file to replace the values in the Web Deploy package with the parameters specified in the Release Manager deployment for the environment.

Visual Studio Project Configuration:

The Visual Studio project is called DemoWebFormsApp

Original connection string value in the Web.config file within Visual Studio:

clip_image001

<connectionStrings>

<add name="SQLServerConn" connectionString="Server=SQL2016\InstanceOne;Database=Shredder;User Id=myUsername;Password=myPassword;" />

Added Parameters.xml file and added to Visual Studio project.

Added parameter value to the Parameters.xml file in Visual Studio with the default value of _SQLServerConn_ with the prefix and suffix of _ in order for the tokenization process to find parameter and change value upon the Release Management deployment.

The other important difference is the addition of the -Web.config Connection String value in the parameter name. This prevents the Web Deploy command from appending the default value during the deployment process.

clip_image002

<parameter name="SQLServerConn-Web.config Connection String" description="doesn't matter" defaultvalue="__SQLServerConn__" tags="">

    <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/connectionStrings/add[@name='SQLServerConn']/@connectionString">

</parameterentry>

</parameter>

Build Definition:

Create the build definition and in the Visual Studio build, add the MSBuild argument /p:DeployOnBuild=true /p:PackageLocation=$(build.artifactstagingdirectory)

OR

/p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation=$(Build.ArtifactStagingDirectory) /p:DeployIisAppPath="Default Web site/FarmToken" /p:DeployOnBuild=true

clip_image003

The MSBuild argument will create the Web Deploy package and generate the Parameters file

clip_image004

Verify the SetParameters.xml file has the correct parameters created and values after the first build.

clip_image005

Release Management Definition:

Download the Colin's ALM Corner Build and Release extension and enable

The release definition will have the following steps:

  • Replace tokens in the SetParameters.xml file with the correct values for the environment
  • Copy the Web Deploy package to a location on the IIS server, like C:\Deployments\Latest
  • Run the Web Deploy package command to update the website files

clip_image006

Replace Token Task:

Within the Replace Token task, select the folder in the Drop folder where the build artifacts are stored:

clip_image007

Change the search pattern to the SetParameters.xml file

clip_image008

The Token Regex pattern is __(\w+)__ in order to find the values in the SetParameter.xml to replace

clip_image009

Copy Files Task:

Set the location of the Web Deploy package on the IIS server. Can either copy to each machine in the farm. Or, copy to gold server and deploy. Test, and then use Web Deploy to copy to the other servers in the farm. This requires an additional batch file for the Web Deploy command.

clip_image010

The target folder is a location on the IIS server where the files are to be copied

clip_image011

Batch File Script to Deploy:

The batch file is created as part of the Web Deploy package creation and this task calls the command with the /Y switch to overwrite files in the web site.

clip_image012

This is the batch file created as part of the Web Deploy package and will have the same name as the web deploy package.

clip_image013

Add the Environment Variables:

For each variable in the Parameters.xml file, add a variable to the environment. The RM tokenizer will replace the value in the Parameters.xml file with the variable value.

Use Release scoped variables in order to reuse the variables for each of the environments:

clip_image014

SQLServerConn is the name of the connection string in the web.config file and the tokenizing task will find _SQLServerConn_ in the SetParameters.xml file and replace it with the value of Server=Server02\InstanceOne;Database=Shredder;User Id=myUsername;Password=myPassword; during the Web Deploy deployment.

Prepare SetParameters.xml:

The DemoWebFormsApp.deploy.cmd will perform the actual web.config updates and other areas of the website at the point of the deployment of the website. The SetParameters.xml file will contain the correct values to apply to the website.

Can use the command file created by the build process, or create own file to use to update the website.

Verify the Replacements in Parameter File was successful:

Check the Replace Tokens task in the logs

clip_image015

SetParameters.xml Final Values

The SetParameters.xml file on the IIS server will have the value from the RM variable.

clip_image016

Update Website w/Web Deploy Command:

The Batch Script executes the

C:\_Agent\LatestDeployments\FarmDemo_Token\LatestPackage\DemoWebFormsApp.deploy

clip_image017

WebSite Web.config Value

Verify the connection string in the web.config contains the correct value:

clip_image018

Fini!