如何為不同的環境合並.net自定義配置節

[英]How to merge .net custom config sections for different environments


Our .net build system uses Enterprise Library 4.1 to replace default web.config settings by calling MergeConfiguration.exe and supplying the original web.config and the delta files for each environment (such as Test and Production) so that the resulting installer packages all environments' specific web.config and installs the correct version based on environment.

我們的.net構建系統使用Enterprise Library 4.1通過調用MergeConfiguration.exe並為每個環境(例如測試和生產)提供原始web.config和delta文件來替換默認的web.config設置,以便生成的安裝程序打包所有環境'具體的web.config並根據環境安裝正確的版本。

This works well for sections known to Enterprise Library, such as the appSettings section. However, we also have some custom sections which I would like to differentiate between environments, such as the following.

這適用於Enterprise Library已知的部分,例如appSettings部分。但是,我們還有一些自定義部分,我想區分環境,如下所示。

For Test

<RoutingSection type="AbcSystem.RoutingSection, AbcSystem"> <Route Source="1" Destination="2" /> ... </RoutingSection>

...

For Production

<RoutingSection type="AbcSystem.RoutingSection, AbcSystem"> <Route Source="1" Destination="3" /> ... </RoutingSection>

...

Ideally the whole custom section is specified in delta files, and during build, replaces its default version from the original web.config.

理想情況下,整個自定義部分在增量文件中指定,並且在構建期間,將其默認版本替換為原始web.config。

Without implementing our own delta merge tool, I have not found a solution. Believing this is a common need for software development, I am seeking for a solution, ideally one that does not involve too many changes to the aforementioned process. It does not have to be Enterprise Library. Thanks in advance.

如果沒有實現我們自己的delta合並工具,我還沒有找到解決方案。相信這是軟件開發的共同需求,我正在尋求一種解決方案,理想情況是不會對上述過程進行太多更改。它不一定是企業庫。提前致謝。

2 个解决方案

#1


0  

The msbuild below will copy your original file and update the value from 2 to 3.

下面的msbuild將復制原始文件並將值從2更新為3。

You will have to install MSBuildCommunityTasks and get the "import project" file name right....but the logic below will work for you.

您必須安裝MSBuildCommunityTasks並獲取“導入項目”文件名....但下面的邏輯將適用於您。

<?xml version="1.0" encoding="utf-8"?>
<Project  ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">

    <!--
  <UsingTask AssemblyFile="$(ProgramFiles)\MSBuild\MSBuild.Community.Tasks.dll" TaskName="Version"/>
  -->
    <Import Project="$(MSBuildExtensionsPath32)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />



    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
    </PropertyGroup>

    <PropertyGroup>
        <DestinationForProductionValue>3</DestinationForProductionValue>
    </PropertyGroup>

    <Target Name="AllTargetsWrapped">
        <CallTarget Targets="CopyItTarget" />
        <CallTarget Targets="WriteXmlPeekValue" />
    </Target>   


    <Target Name="CopyItTarget">
        <Copy SourceFiles="$(WorkingCheckout)\Parameters.xml" DestinationFiles="$(WorkingCheckout)\Parameters_PRODUCTION.xml"/>     
        <Error Condition="!Exists('$(WorkingCheckout)\Parameters_PRODUCTION.xml')" Text="No Copy Is Bad And Sad" />
    </Target>


    <Target Name="WriteXmlPeekValue"  Condition=" '$(DestinationForProductionValue)' != '' ">
        <XmlPoke 
    XmlInputPath="$(WorkingCheckout)\Parameters_PRODUCTION.xml"
            Query="/root/RoutingSection/Route/@Destination"
    Value="$(DestinationForProductionValue)" />
    </Target>   




</Project>

#2


0  

I eventually used SlowCheetah, producing a transformed config file for each environment. It is also TFS build friendly, not requiring changes to the build process. I've also moved our merges previously done using Enterprise Library 4.1 to SlowCheetah, together with the custom sections.

我最終使用了SlowCheetah,為每個環境生成一個轉換后的配置文件。它也是TFS構建友好的,不需要更改構建過程。我還將先前使用Enterprise Library 4.1完成的合並與自定義部分一起移動到SlowCheetah。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/06/25/720a69c0538059710204e1106d292392.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com