如何为几个构建配置选择不同的app.config [英] How to select different app.config for several build configurations

查看:1104
本文介绍了如何为几个构建配置选择不同的app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含MSTest集成测试的 dll类型项目。在我的机器上的测试通过,我想要在CI服务器上发生相同的(我使用TeamCity)。但是测试失败,因为我需要调整app.config中的一些设置。这是为什么我想要有一个单独的第二个app.config文件,将保存CI服务器的设置。



所以我想有

 
/ Sln
/ Proj
app.config(我认为这是VS要求的)
app.Release.config (这是一个独立的独立配置文件)

因此,如果我选择释放配置在CI配置中,我想使用应用程序。 Release.config file instead of app.config



问题

这似乎并不简单.dll类型项目。对于web项目,我可以做Web配置转换。我发现一个黑客如何做一个dll类型项目的这些转换,但我不是黑客的大粉丝。



问题

根据.NET项目的构建配置(例如Debug,Release,...),调整app.config文件的标准方法是什么?

解决方案

使用, MSBuild )将提供转换配置文件的功能取决于配置。如果您在构建机器上构建解决方案,您需要更多地控制哪些内容以及如何准备发布产品,这将非常有用。



使用web发布dll的任务来转换任何配置文件

 < UsingTask AssemblyFile =.. \tools\build\ Microsoft.Web.Publishing.Tasks.dll
TaskName =TransformXml/>

< PropertyGroup>
<! - 输入配置文件的路径 - >
< TransformInputFile> app.config的路径< / TransformInputFile>
<! - 转换文件的路径 - >
< TransformFile>应用程序的路径$(配置).config< / TransformFile>
<! - outptu web配置文件的路径 - >
< TransformOutputFile>输出project.dll.config的路径< / TransformOutputFile>
< / PropertyGroup>

< Target Name =transform>
< TransformXml Source =$(TransformInputFile)
Transform =$(TransformFile)
Destination =$(TransformOutputFile)/>
< / Target>


I have a dll-type project that contains MSTest integration tests. On my machine the tests pass, and I want the same to happen on a CI server (I use TeamCity). But the tests fail, because I need to tweak some settings in app.config. This is why I was thinking to have a separate second app.config file that will hold the settings for CI server.

So I would like to have

/Sln
 /Proj
  app.config (I think this is required by VS)
  app.Release.config (This is a standalone independent config file)

Thus if I select Release configuration in build config on CI, I would like to use app.Release.config file instead of app.config

Problem
This doesn't seem to be straightforward for simple .dll type projects. For web projects, I can do web config transformations. I found a hack how to do these transformations for a dll type project, but I am not a big fan of hacks.

Question
What is a standard approach to tweak app.config files depending on build config for .NET projects (such as Debug, Release, ...)?

解决方案

Use SlowCheetah plugin. For more options and details of how to use SlowCheetah keep reading.

TLDR;

As you have already noticed, there is no default and easy way to use different config files for a Library type (.dll) project. The reason is that the current thinking is: "You don't need to"! Framework developers reckon you need configuration for the executable file: be it a console, desktop, web, mobile app or something else. If you start providing configuration for a dll, you may end up with something I can call a config hell. You may no longer understand (easily) why this and that variables have such weird values coming seemingly from nowhere.

"Hold on", - you may say, "but I need this for my integration/unit testing, and it is a library!". And that is true and this is what you can do (pick only one, don't mix):

1. SlowCheetah - transforms current config file

You can install SlowCheetah - a Visual Studio plug-in that does all low level XML poking (or transformation) for you. The way it works, briefly:

  • Install SlowCheetah and restart Visual Studio (Visual Studio > Tools > Extensions and Updates ... > Online > Visual Studio Gallery > search for "Slow Cheetah" )
  • Define your solution configurations (Debug and Release are there by default), you can add more (right click on the solution in Solution Explorer > Configuration Manager... > Active Solution Configuration > New...
  • Add a config file if needed
  • Right click on config file > Add Transform
    • This will create Transformation files - one per your configuration
    • Transform files work as injectors/mutators, they find needed XML code in the original config file and inject new lines or mutate needed value, whatever you tell it to do

2. Fiddle with .proj file - copy-renames a whole new config file

Originally taken from here. It's a custom MSBuild task that you can embed into Visual Studio .proj file. Copy and paste the following code into the project file

<Target Name="AfterBuild">
    <Delete Files="$(TargetDir)$(TargetFileName).config" />
    <Copy SourceFiles="$(ProjectDir)\Config\App.$(Configuration).config"
          DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>

Now create a folder in the project called Config and add new files there: App.Debug.config, App.Release.config and so on. Now, depending on your configuration, Visual Studio will pick the config file from a Config folder, and copy-rename it into the output directory. So if you had PatternPA.Test.Integration project and a Debug config selected, in the output folder after the build you will find a PatternPA.Test.Integration.dll.config file which was copied from Config\App.Debug.config and renamed afterwards.

These are some notes you can leave in the config files

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

    <!-- This file is copied and renamed by the 'AfterBuild' MSBuild task -->

    <!-- Depending on the configuration the content of projectName.dll.config 
        is fully substituted by the correspondent to build configuration file 
        from the 'Config' directory. -->

</configuration>

In Visual Studio you can have something like this

3. Use scripting files outside Visual Studio

Each build tool (like NAnt, MSBuild) will provide capabilities to transform config file depending on the configuration. This is useful if you build your solution on a build machine, where you need to have more control on what and how you prepare the product for release.

For example you can use web publishing dll's task to transform any config file

<UsingTask AssemblyFile="..\tools\build\Microsoft.Web.Publishing.Tasks.dll"
    TaskName="TransformXml"/>

<PropertyGroup>
    <!-- Path to input config file -->  
    <TransformInputFile>path to app.config</TransformInputFile>
    <!-- Path to the transformation file -->    
    <TransformFile>path to app.$(Configuration).config</TransformFile>
    <!-- Path to outptu web config file --> 
    <TransformOutputFile>path to output project.dll.config</TransformOutputFile>
</PropertyGroup>

<Target Name="transform">
    <TransformXml Source="$(TransformInputFile)"
                  Transform="$(TransformFile)"
                  Destination="$(TransformOutputFile)" />
</Target>

这篇关于如何为几个构建配置选择不同的app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆