Question

I have an ASP.NET Web API project which includes 2 config transforms:

  • Web.Live.config
  • Web.UAT.config

If I choose either Live or UAT configuration when publishing, the transforms are not applied on the rendered web.config file.

I've checked my transform configs, and the name, xdt:Transform and xdt:Locator are correct.

In my web.config I have:

<connectionStrings>
    <add name="foo" providerName="System.Data.SqlClient" connectionString="[main connection string]" />
</connectionStrings>

In my web.Live.config I have:

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

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="foo" 
      connectionString="[live connection string]" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

However my published connection string still displays as the following, even when Live is selected:

<connectionStrings>
    <add name="foo" providerName="System.Data.SqlClient" connectionString="[main connection string]" />
</connectionStrings>

What possible reasons are there for this happening?

Was it helpful?

Solution

It seems that the problem in this case is assuming that changing the build configuration profile will automatically update your Publish Profile to match - this is not the case.

If you have a single Publish Profile then you will have to manually change the "Settings" > "Publish" > "Configuration" setting each time you make a build, as this is what determine which config transform file is applied.

The recommended way to handle this is to create multiple Publish Profiles - one for each build configuration. For example, "Debug" and "Release", or in this case "Live" and "UAT". This way you can easily switch between the two profiles during the Publish process.

It is worth noting that there are other benefits to this approach than just managing your transforms. Having different profiles for different builds allows you to specify different targets too. For example, if you use the Web Deploy method you can send the application to different servers depending on your target environment (e.g. Testing or production)

OTHER TIPS

I had a similar problem, where the applied transformations would be the ones from "Debug", not from my "Test" configuration. I solved it by manually editing some files. I am not sure which ones were wrong, and how they got wrong, but this is how I solved it.

I changed the .sln from "Debug":

    {96...D2A}.Test|Any CPU.ActiveCfg = Debug|Any CPU
    {96...D2A}.Test|Any CPU.Build.0 = Debug|Any CPU
    {96...D2A}.Test|Mixed Platforms.ActiveCfg = Debug|Any CPU
    {96...D2A}.Test|Mixed Platforms.Build.0 = Debug|Any CPU
    {96...D2A}.Test|x86.ActiveCfg = Debug|Any CPU

to "Test":

    {96...D2A}.Test|Any CPU.ActiveCfg = Test|Any CPU
    {96...D2A}.Test|Any CPU.Build.0 = Test|Any CPU
    {96...D2A}.Test|Mixed Platforms.ActiveCfg = Test|Any CPU
    {96...D2A}.Test|Mixed Platforms.Build.0 = Test|Any CPU
    {96...D2A}.Test|x86.ActiveCfg = Test|Any CPU

I changed the Test-configuration in the .csproj from:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|AnyCPU'">
  <DebugSymbols>true</DebugSymbols>
  <OutputPath>bin\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <DebugType>full</DebugType>
  <PlatformTarget>AnyCPU</PlatformTarget>
  <ErrorReport>prompt</ErrorReport>
  <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

to (I am not sure what all these removed parameters mean/imply):

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|AnyCPU'">
  <OutputPath>bin\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

I changed the Web.Test.config in the .csproj from:

<None Include="Web.Test.config">
  <DependentUpon>Web.config</DependentUpon>
</None>

To:

<Content Include="Web.Test.config">
  <DependentUpon>Web.config</DependentUpon>
</Content>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top