Saturday, July 7, 2012

Web.Config transformations

Add setting to web.config and then create one configuration file per environment e.g. web.DEV.config,web.Test.Config,web.UAT.config,web.Production.Config

Each of these configuration files will contain environment specific settings which we want to apply to base web.config such as connection strings,service endpoints etc.

To generate final web.config, we need to set xml transformation rules in  these web.config.
e.g.

<add name="Test" connectionString="newConnectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />

<replaceAll>
    <endpointAddresses
       xdt:Locator="XPath(/configuration/system.serviceModel//*@contract='Contract1Name'])"
       xdt:Transform="SetAttributes(address)" address="newEndpointAddress" />      
 </replaceAll>

If we want to replace entire section:

<system.web>
    <compilation targetFramework="4.0" xdt:Transform="Replace">
      <assemblies>
        <add assembly="AssemblyName, Version=VersionInfo, Culture=neutral,       PublicKeyToken=publicToken" />
      </assemblies>
    </compilation>
</system.web>
<add name="Test" connectionString="newConnectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />

TFS Build Arguments to deploy a build

Build arguments:

/p:Configuration=ConfigurationName
/p:DeployOnBuild=True
/p:DeployTarget=MsDeployPublish
 /p:CreatePackageOnPublish=False
/p:MSDeployPublishMethod=RemoteAgent
  /p:AllowUntrustedCertificate=True
/p:MSDeployServiceUrl=ServerName
/p:DeployIisAppPath=ApplicationName
/p:UserName=UserName /p:Password=Password