Web Deployment through Visual Studio

This blog explains the 2 different ways of deploying the web application to the IIS server.

We often come across the scenarios where in the application's web.config need to have different settings when it is deployed from one environment to the other (Example: From development to staging or production).

We can use either of the below technique to automate the process of changing web.config during deployment:

Below is the list of a few settings which we tend to change while deploying application from one environment to the other:

  1. appSettings
  2. connectionStrings
  3. debug
  4. customErrors
  5. runAllManagedModulesForAllRequests

Web.config tranformation:

This technique specifies how the web.config file should be changed based on the specific build configurations:

  • Debug
  • Release
  • Custom build configurations. 

The web.config of the above build configuration makes use of the XML-Document-Transform namespace defines two attributes:

  • Locator: Thisattribute specifies the web.config element or set of elements that you want to change in some way.
  • Transform: This attribute specifies what you want to do to the elements that Locator attribute finds.

 To achieve the above requirements using the web.config transformation technique, we need to follow the below steps:

  • Right Click on the Application in Visual Studio and click on Properties.
  • Select the appropriate Build configuration and the path where you would like the zip file to be created.
  • In this scenario we have selected the configuration as Release. So the default location where the zip file will be created is application relative path Obj\Release\Package\.

  • Below is the sample content placed in the web.Release.config which will achieve the requirement:

    <appSettings>

        <add key="Title" value="Titletest"

         xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />

    </appSettings>

    <connectionStrings>

        <add name="testConnectionString"

         connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"

         xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

    </connectionStrings>

    <system.web>

        <compilation xdt:Transform="RemoveAttributes(debug)" />

        <customErrors mode="On" defaultRedirect="GenericError.htm" xdt:Transform="Replace">

            <error statusCode="500" redirect="InternalError.htm"/>

        </customErrors>

    </system.web>

    <system.webServer>

        <modules xdt:Transform="SetAttributes" runAllManagedModulesForAllRequests="false">

  • If You are using Visual Studio 2010 then, Right click on the application and selectBuild Deployment Package:

On Visual Studio 2012 and above You can go to Build-àPublish Selection and select the below setting:

 Once the .zip package gets created in the above mentioned location, You can either move the package manually to the destination server and import it using the IIS GUI (shown below) or use the web deploy and option in Visual studio and publish it directly.

 

Web Deploy parameters:

This technique is useful when you have to create a package when you do not know what values need to be set until you deploy the application to the different environment. Moreover this technique also helps when the same application (package .zip file) needs to be placed in different environments with different values.

 To achieve the above requirements using the Web Deploy parameters technique, we need to follow the below steps:

  1. Create a file named parameters.xml and save it in the projects folder.

  2. Under the File Properties of parameters.xml set the Build Action to None. This way the parameters.xml file would not be copied along with the web site contents.

  3. Build the deployment package using the steps mentioned here

  4. The package (.zip) file will be generated in the specified location. You will also find "SetParameters.xml" file which can be modified with required parameters (values) before deploying them to the destination server.

  5. Finally execute the below to deploy the application. Alternatively you may use the "deploy.cmd" file to deploy the package to destination server.

     

You can also import the package using the msdeploy command:

msdeploy.exe -source:package='C:\WebDeploy_Demo.zip' -dest:auto,computerName='<destination server>',includeAcls='False' -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"C:\WebDeploy_Demo.SetParameters.xml"

 

If you use IIS Manager to import the package from the IIS GUI:

Once imported you are prompted to enter a value for the parameter in the Enter Application Package Information dialog box. The dialog box displays the name, description, and default value that you specified, as shown below:

 Below is the sample content placed in the parameters.xml which will achieve the requirement:

<?xml version="1.0" encoding="utf-8" >

<parameters>

      <parameter name="connectionstring"

        description="Changing the SQL connectionString to point to the production DB"

        defaultValue="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"

        tags="SqlConnectionString">

         <parameterEntry scope="\\web.config$"

         match="/configuration/connectionStrings/add[@name='mstestConnectionString1']/@connectionString" />

     </parameter>

     <parameter name="appsettings"

         description="Changing the appsettings value"

         defaultValue="Titletest"

         tags="" >

         <parameterEntry

            scope=\\web.config$

            match="/configuration/appSettings/add[@key='Title']/@value" />

     </parameter>

     <parameter name="Debug value"

         description="Changing the debug value in web.config"

         defaultValue="false"

         tags="">

         <parameterEntry

             scope=\\web.config$

             match=/configuration/system.web/compilation/@debug />

    </parameter> 

    <parameter name="runAllManagedModulesForAllRequests setting for modules"

          description="Change the runAllManagedModulesForAllRequests in web.config"

          defaultValue="false"

          tags="">

        <parameterEntry

              scope=\\web.config$

              match=/configuration/system.webServer/modules/@runAllManagedModulesForAllRequests />

     </parameter>

</parameters>

 

Reference Articles:

https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/dd465326.aspx

https://msdn.microsoft.com/en-us/library/ff398068.aspx