Johnathon Sullinger
Johnathon Sullinger

Reputation: 7414

App.config transforms aren't applied to AppName.exe.config

I have an app.config file in our WPF project that is transformed using the following build target in the .csproj

<!-- MSbuild Task for transforming app.config based on the configuration settings -->
<UsingTask TaskName="TransformXml" 
           AssemblyFile="$(SolutionDir)\packages\MSBuild.Microsoft.VisualStudio.Web.targets.12.0.4\tools\VSToolsPath\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterBuild">
  <!-- Transform the app.config into the correct config file associated to the current build settings -->
  <TransformXml Source="App.config" Transform="App.$(Configuration).config" Destination="$(OutputPath)\App.config" />
  <!-- Bit of .Net to get all folders and subfolders that we want to delete post-build -->
  <ItemGroup>
    <FoldersToClean Include="$([System.IO.Directory]::GetDirectories(&quot;$(OutputPath)&quot;))" />
  </ItemGroup>
  <!-- Delete all of our localization folders and .pdb symbols. We only delete PDB files for Release builds. Debug builds we will leave them. -->
  <RemoveDir Directories="@(FoldersToClean)" />
  <Exec Command="del $(OutputPath)*.pdb" Condition=" '$(Configuration)'=='Release' " />
</Target>

This correctly generates an App.config after the compilation, with my transformed QA or Production configuration files created. The issue I ran into however is that the following code uses AppName.exe.config.

string encryptedString = ConfigurationManager.AppSettings["SqlConnection"];

After doing a bit of reading, I understand why it's doing that. The App.config file ultimately becomes the AppName.exe.config for use during runtime. That's fine; my transformation happens to late though. When the compilation is completed, the AppName.exe.config file contains my base App.config file information, and none of the transformed settings. I assume that is due to the transform happening as a post-build step, where it transforms the App.config after the original App.config file was used to generate AppName.exe.config.

It looks like there isn't any thing preventing me from changing

<TransformXml Source="App.config" 
              Transform="App.$(Configuration).config" 
              Destination="$(OutputPath)\App.config" />

so that it replaces the AppName.exe.config file post-build.

<TransformXml Source="App.config" 
              Transform="App.$(Configuration).config" 
              Destination="$(OutputPath)\AppName.exe.config" />

Is there anything wrong with this? There isn't much in the way of help when using app.config and transforms for desktop applications. Everything I've read online is for transforming things into a web-config file. I assume this is safe and more or less is the same thing. However I wanted to make sure I'm not missing any glaring side-effects or issues i'll encounter by replacing the original AppName.exe.config with the transformed App.config.

Upvotes: 1

Views: 573

Answers (1)

Jason Dimmick
Jason Dimmick

Reputation: 500

Install this https://github.com/acottais/msbuild.xdt NuGet package and you will be able to create transforms like a web.config.

There are several other Nuget packages that will transform the app.config just like the web.config.

I used this one the other day and it worked great.

Upvotes: 1

Related Questions