English 中文(简体)
如何选择不同的组合。
原标题:How to select different app.config for several build configurations

我有一个dll-type Project, 其中包含管理系统测试一体化。 在我的机器上,测试通过后,我希望在 CI服务器上同样发生。 但是,这些测试失败了,因为我需要把某些环境推向 app。 因此,我想另立第二套书,为CI服务器提供场所。

因此,我本希望

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

因此,如果我选择“释放组合”在“ CI”上建树,我就希望使用“ app”。 释放。 混淆文件,而不是照相。

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, ...)?

最佳回答

http://udiostgallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5“rel=“noreferer”>。 关于如何使用SlowCheetah的更多备选办法和细节,请随时阅读。

如你已经注意到的那样,对于图书馆类型(dll)项目,没有缺省办法使用不同的组合文件。 原因是目前的想法是:“你不需要”! 框架制定者认为,你需要的是可执行档案的配置:它是一种专线、台式、网络、移动器或其他东西。 如果你开始为dll<>>/em>提供配置,你可以最后用我可以称之为config hell的内容。 你们可能不再理解(很简单)为什么这一点,以及这些变数似乎来自任何地方的这种超常的价值观。

“过去”——你可以说,“但我需要做我的整合/unit测试,t>是一个图书馆”。 确实如此,这是你可以做的(仅举一例,不混杂):

1. SlowCheetah - transforms current config file

您可安装SlowChetah - 视觉演播室插播,为你提供低水平XML p(或转化)。 The way of it work, brief:

  • 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

最初摘自here。 它是一项惯例,由您将这项任务纳入视觉演播室.proj档案。 项目档案中复制和编辑以下代码

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

现在,在名为<代码>Config的项目中创建了一个文件夹,并在其中添加新的文件:App.Debug.config,App.Release.config等。 现在,视您的配置而定,视频演播室将从<代码>Config的复印件中选取书面材料,并将其贴在产出目录中。 因此,如果您选择了PatternPA. 试验.Integration <>m>项目和Debugconfig,将找到一个PatternPA. 试验.Integration.dll.config文档,该文档由复制。 ConfigApp.Debug.config, 后改名。

这些是你可以在汇合档案中留下的一些笔记。

<?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>

在视觉演播室,你可以有这样的东西。

“Project

3. Use scripting files outside Visual Studio

每个建筑工具(如,)将提供根据配置改变编组文档的能力。 如果你在建筑机器上找到解决办法,那么这样做是有用的,因为你需要更多地控制产品的内容和如何准备释放。

例如,你可以利用网上印刷机改造任何集束文档。

<UsingTask AssemblyFile="..	oolsuildMicrosoft.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>
问题回答

You can try the following approach:

  1. Right-click on the project in Solution Explorer and select Unload Project.
  2. The project will be unloaded. Right-click on the project again and select Edit <YourProjectName>.csproj.
  3. Now you can edit the project file inside Visual Studio.
  4. Locate the place in *.csproj file where your application configuration file is included. It will look like:
    <ItemGroup>
        <None Include="App.config"/>
    </ItemGroup>
  1. Replace this lines with following:
    <ItemGroup Condition="  $(Configuration)  ==  Debug  ">
        <None Include="App.Debug.config"/>
    </ItemGroup>

    <ItemGroup Condition="  $(Configuration)  ==  Release  ">
        <None Include="App.Release.config"/>
    </ItemGroup>

我没有尝试过这一办法处理<条码>应用程序。 你们几乎可以像你一样,对建筑过程进行定制。 无论如何,让我知道结果。

采用与罗马相同的做法,我将其改编为2010年视觉演播室:

 <None Condition="  $(Configuration)  ==  Debug  " Include="appDebugApp.config" />

 <None Condition="  $(Configuration)  ==  Release  " Include="appReleaseApp.config" />

Here you need to keep both App.config files in different directories (appDebug and appRelease). I tested it and it works fine!

页: 1 Config Gen。 它是为此目的制定的。 该系统根据模板文档和环境档案,为每个部署机器编制一个配置文件。 我知道,这并没有具体回答你的问题,但它可能很好地回答了你的问题。

因此,而不是欺骗, 释放等,你可能有测试、UAT、生产等。 各位还能为每个开发机配备不同的环境,以便你能够产生一个专门针对你的开发机的配置,并在不影响任何其他部署的情况下加以改变。

一种使用的例子可能是......

<Target Name="BeforeBuild">
    <Exec Command="C:Toolscfg -s $(ProjectDir)App.Config.Settings.xls -t       
        $(ProjectDir)App.config.template.xml -o $(SolutionDir)ConfigGen" />

    <Exec Command="C:Toolscfg -s $(ProjectDir)App.Config.Settings.xls -t
        $(ProjectDir)App.config.template.xml -l -n $(ProjectDir)App.config" />
</Target>

如果你将此事列入你档案,你有以下档案......

$(ProjectDir)App.Config.Settings.xls

MachineName        ConfigFilePath   SQLServer        

default             App.config      DEVSQL005
Test                App.config      TESTSQL005
UAT                 App.config      UATSQL005
Production          App.config      PRODSQL005
YourLocalMachine    App.config      ./SQLEXPRESS


$(ProjectDir)App.config.template.xml 

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
   <configuration>
   <appSettings>
       <add key="ConnectionString" value="Data Source=[%SQLServer%]; 
           Database=DatabaseName; Trusted_Connection=True"/>
   </appSettings>
</configuration>

......结果......

从第一指挥部看,在Xls档案中为每个环境生成的汇合文档,列入产出目录(SolutionDir)

.../solutiondir/ConfigGen/Production/App.config

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
   <configuration>
   <appSettings>
       <add key="ConnectionString" value="Data Source=PRODSQL005; 
           Database=DatabaseName; Trusted_Connection=True"/>
   </appSettings>
</configuration>

From the second command, the local App.config used on your dev machine will be replaced with the generated config specified by the local (-l) switch and the filename (-n) switch.

我听到了有关SlowCheetah的好消息,但无法工作。 我做了以下工作:为具体组合增加每个组合的标记。

Ex:

<PropertyGroup Condition=" $(Configuration)|$(Platform)  ==  UAT|AnyCPU ">
    <OutputPath>binUAT</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AppConfig>App.UAT.config</AppConfig>
  </PropertyGroup>

I m using XmlPreprocess tool for config files manipulation. It is using one mapping file for multiple environments(or multiple build targets in your case). You can edit mapping file by Excel. It is very easy to use.

视觉艺术馆Slow Cheetah和Feng Koala似乎是帮助解决这一问题的良好工具。

然而,如果你想避免在你的整个建设/融合过程中更广泛地执行这些原则,那么,在你所编的“proj”档案中添加这一内容是短篇的。

注:这多或少是@oleksii的答复第2号。

这些项目包括:

  <Target Name="TransformOnBuild" BeforeTargets="PrepareForBuild">
    <TransformXml Source="App_Configapp.Base.config" Transform="App_Configapp.$(Configuration).config" Destination="app.config" />
  </Target>

这一项目旨在:

  <Target Name="TransformOnBuild" BeforeTargets="PrepareForBuild">
    <TransformXml Source="App_ConfigWeb.Base.config" Transform="App_ConfigWeb.$(Configuration).config" Destination="Web.config" />
  </Target>

指出,这一步骤甚至在适当开始之前就已发生。 组合文档的转变发生在项目夹中。 因此,网络已经转变。 当你打碎时(SlowCheetah的背部)便可动用。

Do remember that if you create the App_Config folder (or whatever you choose to call it), the various intermediate config files should have a Build Action = None, and Copy to Output Directory = Do not copy.

这把这两种选择结合起来。 适当者根据条件执行。 第一阶段确定“转变Xml”的任务:

<Project>
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)MicrosoftVisualStudiov$(VisualStudioVersion)WebMicrosoft.Web.Publishing.Tasks.dll" />
<Target Name="TransformOnBuild" BeforeTargets="PrepareForBuild">
    <TransformXml Condition="Exists( App_Configapp.Base.config )" Source="App_Configapp.Base.config" Transform="App_Configapp.$(Configuration).config" Destination="app.config" />
    <TransformXml Condition="Exists( App_ConfigWeb.Base.config )" Source="App_ConfigWeb.Base.config" Transform="App_ConfigWeb.$(Configuration).config" Destination="Web.config" />
</Target>

I have solved this topic with the solution I have found here: http://www.blackwasp.co.uk/SwitchConfig.aspx

In short what they state there is: "by adding a post-build event.[...] We need to add the following:

if "Debug"=="$(ConfigurationName)" goto :nocopy
del "$(TargetPath).config"
copy "$(ProjectDir)Release.config" "$(TargetPath).config"
:nocopy

如果XDT(web.config)改造发动机能够帮助你。 目前,它只是本土支持网络项目,但从技术上看,没有任何东西阻止你在其他应用类型中使用。 关于如何用人工编辑XDT的多种指南,但我发现了一个大片:

原始材料只是帮助建立配置,不需要建造,解决办法可以建立在其他机器或建筑服务器上,而不需要花钱或任何其他工具。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...