Web.config-style transforms for App.config files for C# Console apps?
Asked Answered
G

5

7

My team is in love with publish profiles. We have a number of testing environments that we work in, and using Web.config transforms we're able to substitute the names of machines in each environment, configuration settings, etc, which makes our build/deploy/test process much easier.

For example:

Web.config
    Web.dev.config
    Web.int.config
    Web.prod.config

However, we have a number of test applications that we use to hit our web services, implemented as Console applications. We'd like to be able to do the same type of transforms on these config files, so that we can reduce manual labor of testing related to manual editing of config files when picking up a build drop.

Essentially, we want this:

App.config
    App.dev.config
    App.int.config
    App.prod.config

My understanding of these config transforms is that they're related to the corresponding publish profiles in our web projects:

Properties
    PublishProfiles
        dev.pubxml
        int.pubxml
        prod.pubxml

I've attempted to add similar files to our Console app projects, but I suspect it's something in the web publishing MSBuild targets that actually utilizes them.

Is there a way to hook into this part of the build specifically, so that we can transform non-web configs?

Gigantes answered 18/10, 2013 at 18:1 Comment(0)
W
9

To add multiple app.configs to a console application:

I am using visual studio 2013. In this version we don't have a config transformation. Please install the configuration transformation extension.

First add environments in solution configuration. Right click on the App.config file. Click on the Add config Transform. It adds as many as solution configurations.

In App.config:

<appSettings>
    <add key="key" value="some value" />   
</appSettings>

Copy this code in App.Release.Config:

<appSettings>
    <add key="same key which you have given in app.config" value="some new value" xdt:Transform="Replace" xdt:Locator="Match(key)" />   
 </appSettings>

Finally, build the solution.

Wood answered 6/2, 2014 at 13:13 Comment(0)
C
6

I was looking for an easy solution for Azure WebJobs project to transform App.config during build that works with Visual Studio and MSBuild on the server.

For Azure WebJobs projects, following addition to .csproj helped:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="Exists('App.$(configuration).config')">
    <Message Text="Transforming config files..." Importance="high" />
    <TransformXml Source="App.config" Transform="App.$(Configuration).config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" />
    <PropertyGroup>
      <AppConfig>$(IntermediateOutputPath)$(TargetFileName).config</AppConfig>
    </PropertyGroup>
    <ItemGroup>
        <AppConfigWithTargetPath Remove="App.config" />
        <AppConfigWithTargetPath Include="$(AppConfig)" Condition="'$(AppConfig)'!=''">
            <TargetPath>$(TargetFileName).config</TargetPath>
        </AppConfigWithTargetPath>
    </ItemGroup>
</Target>

Works with build, no extensions required and App.config is not overwritten in place. Based on Dhia Louhichi's answer.

Chaldron answered 26/6, 2017 at 2:29 Comment(0)
E
3

Simplest solution I've found for this is using Slowcheetah; they have tutorials on their website for using MSBuild, etc, I believe for switching properly after pushing from Dev to QA, etc.

http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

It just works for me :)

Endbrain answered 18/10, 2013 at 18:3 Comment(2)
Isn't transformation of web.config already integrated in VS2012? I am interested in knowing why you use a 3rd party addon.Loosing
For web.config, yes. Not for App.config, and not for VS2010 (which I do occasionally use).Endbrain
A
1

I suggest this blog post:

http://blogs.msdn.com/b/webdev/archive/2010/11/17/xdt-web-config-transforms-in-non-web-projects.aspx

It explains how to use the xdt transformation also with WPF projects configuration files. I followed these instructions for some console applications and it works very well. I'm using VS2012 Express, but I suppose it will work also for VS2010.

Apprehensive answered 17/2, 2014 at 16:37 Comment(0)
B
0

Recently I set up a build server and this is what I ended up using:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="BeforeBuild" Condition="">
    <Message Text="Transforming Configs files..." Importance="high" />
    <TransformXml Source="App_Data\config\appSettings.config" Transform="App_Data\config\appSettings.$(Configuration).config" Destination="App_Data\config\appSettings_temp.config" />
    <Copy SourceFiles="App_Data\config\appSettings_temp.config" DestinationFiles="App_Data\config\appSettings.config" OverwriteReadOnlyFiles="True" />
    <Delete Files="App_Data\config\\appSettings_temp.config" />
  </Target>

Explanation:

  1. Import the TransformXml Task through UsingTask (Make sure you set the right Visual studio task path)
  2. Display a custom message. (You can set the Importantance to High, normal, low)
  3. Call the TransformXml task and create a temporary file.
  4. Copy the temporary file to the main one. You need OverwriteReadOnlyFiles as TFS will probably lock it.
Baliol answered 16/3, 2017 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.