Web.config is not transformed when published

614 Views Asked by At

I am trying to apply config transformation in one of my C# projects. I can see the transformation applied when I do "Preview transform" on my QA config file but when I do publish with QA profile, I can still see the default config (not the transformed one) in deployment. I do not want the transformation on the build (I know slowcheetah helps in that), I would need that on publishing only. Is there any changes we need to do in the .csproj file to enable the transformations on publishing.

EDIT: Adding the sample config request by gkb. We have entry in web.config:

<connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\xxxx.mdf;Initial Catalog=xxxx;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

AND I have below in my web.Debug-QA.config:

    <?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="DefaultConnection" 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)" />
  </system.web>
</configuration>
1

There are 1 best solutions below

1
Daniel On

In our projects we have two different steps:

  1. transform with slow cheetah on build time
    (i know you wrote you do not want to use it but give it a try)

On build time we create tokens in our configs. app.release file looks like this:

<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="InstrumentationKey" value="__InstrumentationKey__" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

E.g. after a release build our app config looks like this:

<configuration>
    <appSettings>
        <add key="InstrumentationKey" value="__InstrumentationKey__" />
    </appSettings>
</configuration>
  1. on release time we do a replacement for the tokens for each specific environment

I don't know your release process... But in our case we have a list of replacements for each environment.

When we do a release all the Tokens (the guys with the two underscores) get replaced with the specific values for each environment.