Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between solution configuration, publish profile, and web.config transforms

I have the following setup in a Visual Studio 2013 ASP.NET Web API 2 project.

  • Web.Develop.config web transform to set an app settings key value
  • Web.Release.config web transform to remove an app settings key
  • Develop.pubxml to map to the Web.Develop.config transform
  • Release.pubxml to map to the Web.Release.config transform

Details for each are found below.

<!-- Web.Develop.config (Web Config Transform) -->
<appSettings>
  <add key="ReportInputPath" 
       value="DevelopPath" 
       xdt:Transform="SetAttributes" 
       xdt:Locator="Match(key)" />
  </appSettings>
<!-- Web.Release.config (Web Config Transform) -->
<appSettings xdt:Transform="Remove" />
<!-- **Develop.pubxml (Publish Profile) -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>x64</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>Path</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles
    <ExcludeFilesFromDeployment>packages.config</ExcludeFilesFromDeployment>
  </PropertyGroup>
</Project>
<!-- Release.pubxml (Publish Profile) -->
<!-- Contents are identical to Develop.pubxml. 
     This is used to target the Web.Release.Config transform. -->

Whenever I publish the application via the Release publish profile my <appSettings/> element is successfully removed. However, <appSettings/> element is removed when the Develop publish profile is run as well.

What I want to understand is:

Why is the <appSettings/> element being removed when I run the Develop publish profile instead of setting the ReportInputPath value?

And what are the relationships between the between solution/project configurations, publish profiles, and web.config transforms?

like image 609
Ryan Taylor Avatar asked Jul 31 '15 02:07

Ryan Taylor


1 Answers

The answer to why the <appSettings/> element is being removed when the Develop publish profile is run is because two transformations are run in the following order.

  1. Web.Release.config. This is run because the configuration target in the Develop.pubxml file is the Release build configuration.
  2. Web.Develop.config. This is run because the name of the publish profile (Develop) matches the name of the transform file.

What is happening is the the first transformation removes the <appSettings/> element. The second transformation attempts to set the key value in that element, but cannot find it, so it silently fails.

I was able to confirm this by searching through the console output. When the Develop transformation was run there was a warning that the desired element could not be found.

Example (shortened for clarity)
> TransformXml: Applying Transform File: C:\...\MyProject\Web.Develop.config
> C:\...\MyProject\Web.Develop.config(6,4): Warning : No element in the source document matches '/configuration/appSettings'
> TransformXml: Not executing SetAttributes (transform line 9, 10)

The Profile specific web.config transforms and transform preview article by Sayed Ibrahim Hashimi was very helpful in identifying this was the issue.

As far as the relationship between the build configuration, publish profiles, and web.config transform go my current understanding is this.

  1. Publish profiles have (among other things) a configuration target
  2. Publish profiles first run the transformation that maps to the their specified configuration target name if one exists
  3. Publish profiles then run the transformation that maps to their publish profile name if one exists

The key here being that two web.config transformations may be run.

like image 180
Ryan Taylor Avatar answered Oct 05 '22 22:10

Ryan Taylor