[英]How to modify custom sections of the web.config file, from a generic .NET application?
[英]How to merge .net custom config sections for different environments
我们的.net生成系统使用Enterprise Library 4.1来替换默认的web.config设置,方法是调用MergeConfiguration.exe并为每个环境(例如测试和生产)提供原始的web.config和增量文件,以便最终的安装程序打包所有环境特定的web.config并根据环境安装正确的版本。
这对于企业库已知的部分(例如appSettings部分)效果很好。 但是,我们也有一些自定义部分,我想在环境之间进行区分,例如以下内容。
测试
<RoutingSection type="AbcSystem.RoutingSection, AbcSystem"> <Route Source="1" Destination="2" /> ... </RoutingSection>
用于生产
<RoutingSection type="AbcSystem.RoutingSection, AbcSystem"> <Route Source="1" Destination="3" /> ... </RoutingSection>
理想情况下,整个自定义部分都在增量文件中指定,并且在构建过程中,从原始web.config中替换其默认版本。
没有实现我们自己的增量合并工具,我还没有找到解决方案。 相信这是软件开发的普遍需求,我正在寻找一种解决方案,理想情况下,该解决方案不需要对上述过程进行太多更改。 它不必是企业库。 提前致谢。
下面的msbuild将复制您的原始文件,并将值从2更新为3。
您将必须安装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>
我最终使用了SlowCheetah,为每个环境生成了一个转换后的配置文件。 它也是TFS构建友好的,不需要更改构建过程。 我还将以前使用Enterprise Library 4.1完成的合并以及自定义部分移到了SlowCheetah。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.