如何选择不同的app.config数生成配置 [英] How to select different app.config for several build configurations

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

问题描述

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

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.

所以,我想有


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

因此​​,如果我选择在CI构建配置版本的配置,我想用app.Release.config文件而不是的app.config

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

问题
但这似乎不是简单的简单的.dll文件类型的项目。对于Web项目,我所能做的Web配置转换。我发现一个黑客怎么做这些转换为一个DLL类型的项目,但我不是黑客的忠实粉丝。

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.

问题
什么是标准的方法来调整的app.config取决于构建配置的.NET项目(如调试,发布,...)?

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

推荐答案

使用的 SlowCheetah 插件。欲了解更多选项和如何使用SlowCheetah细节请继续阅读。

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

TLDR;

正如你已经注意到了,不存在违约和简单的方法来使用不同的配置文件的库类型文件(.dll)的项目。其原因是,目前减薄是:你并不需要!框架开发估计需要配置的可执行文件:无论是一个控制台,桌面,网络,移动应用程序或别的东西。如果你开始了的 DLL 的,你可能最终的东西提供配置我可以调用的配置地狱的。您可能不再了解(很容易),为什么这样那样的变量有这样奇怪的价值观不知从哪儿来了看似。

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 thining 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):

您可以安装 SlowCheetah - 一个Visual Studio插件在完成所有低级XML戳(或改造)为您。它的工作方式,简单地说:

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:

  • 安装SlowCheetah并重新启动Visual Studio
  • 定义您的解决方案配置(调试发布的是有默认),你可以添加更多(右键点击的解决方案资源管理器> 配置管理器... 的>的活动解决方案配置的>的新建...
  • 如果需要添加一个配置文件
  • 右键单击配置文件>的添加变换
    • 这将创建转换文件 - 根据您的配置一个
    • 在转换文件的工作为喷油器/变化器,他们发现所需的XML code在原来的配置文件,并注入新的线路或变异所需的价值,不管你告诉它做
    • Install SlowCheetah and restart Visual Studio
    • 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

      最初取自这里。这是一个自定义的MSBuild任务,你可以嵌入到Visual Studio中的 .proj 的文件。复制并粘贴以下code到项目文件

      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>
      

      现在创建一个名为项目配置文件夹,并添加新的文件有: App.Debug.config App.Release的.config 的等。现在,根据您的配置,Visual Studio将挑选一个配置文件夹中的配置文件,并复制,重命名到输出目录。所以,如果你有 PatternPA.Test.Integration 的项目和调试的配置选择,在构建后的输出文件夹,你会发现一个的 PatternPA.Test.Integration。 dll.config 的,将其从配置\ App.Debug.config 复制和重命名后的文件。

      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>
      

      在Visual Studio中,你可以有这样的事情

      In Visual Studio you can have something like this

      每个构建工具(如,的的MSBuild )将提供的功能转换的配置文件取决于配置。这个,如果你建立你构建的机器,你需要有更多的控制上的解决方案是非常有用的东西,你怎么prepare的产品发布。

      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.

      例如,您可以使用网上发布的DLL的任务是将任何配置文件

      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天全站免登陆