Ami*_*abh 528 app-config .net-4.0 visual-studio web-config-transform slowcheetah
对于Visual Studio 2010基于Web的应用程序,我们具有Config Transformation功能,通过该功能,我们可以为不同的环境维护多个配置文件.但是,对于Windows Services/WinForms或控制台应用程序,App.Config文件无法使用相同的功能.
这里提供了一种解决方法:将XDT魔术应用于App.Config.
然而,它并不简单,需要许多步骤.有没有更简单的方法来实现相同的app.config文件?
Dan*_*mov 548
我尝试了几种解决方案,这是我个人发现的最简单的方法.
Dan在评论中指出,原帖属于Oleg Sych - 谢谢,Oleg!
以下是说明:
1.为项目添加每个配置的XML文件.
通常,您将拥有Debug
和Release
配置,因此命名您的文件App.Debug.config
和App.Release.config
.在我的项目中,我为每种环境创建了一个配置,因此您可能想要尝试一下.
2.卸载项目并打开.csproj文件进行编辑
Visual Studio允许您在编辑器中编辑.csproj文件 - 您只需要先卸载项目.然后右键单击它并选择Edit <ProjectName> .csproj.
3.将App.*.配置文件绑定到主App.config
找到包含所有App.config
和App.*.config
引用的项目文件部分.您会注意到他们的构建操作设置为None
:
<None Include="App.config" />
<None Include="App.Debug.config" />
<None Include="App.Release.config" />
Run Code Online (Sandbox Code Playgroud)
首先,为所有人设置构建操作Content
.
接下来,使所有特定于配置的文件都依赖于主要文件,App.config
因此Visual Studio将它们分组为设计器和代码隐藏文件.
将上面的XML替换为下面的XML:
<Content Include="App.config" />
<Content Include="App.Debug.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
<Content Include="App.Release.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
Run Code Online (Sandbox Code Playgroud)
4.激活变形魔法
在文件结束之后
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Run Code Online (Sandbox Code Playgroud)
在决赛之前
</Project>
Run Code Online (Sandbox Code Playgroud)
插入以下XML:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="CoreCompile" Condition="exists('app.$(Configuration).config')">
<!-- Generate transformed app config in the intermediate directory -->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
Run Code Online (Sandbox Code Playgroud)
现在您可以重新加载项目,构建它并享受App.config
转换!
FYI
确保您的App.*.config
文件具有如下正确的设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--magic transformations here-->
</configuration>
Run Code Online (Sandbox Code Playgroud)
Sco*_*man 407
现在可以使用本文中处理的Visual Studio AddIn:SlowCheetah - 现在为任何XML配置文件推广的Web.config转换语法.
您可以右键单击web.config并单击"添加配置转换".执行此操作时,您将获得web.debug.config和web.release.config.如果您愿意,可以创建web.whatever.config,只要名称与配置文件一致即可.这些文件只是您想要的更改,而不是web.config的完整副本.
您可能认为您想要使用XSLT来转换web.config,但是虽然它们看起来很直观,但它实际上非常冗长.
这是两个转换,一个使用XSLT,另一个使用XML Document Transform语法/命名空间.与所有事情一样,XSLT中有多种方法可以做到这一点,但是你会得到一般的想法.XSLT是一种通用的树转换语言,而此部署版本则针对特定场景的特定子集进行了优化.但是,很酷的部分是每个XDT转换都是一个.NET插件,所以你可以创建自己的.
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="/configuration/appSettings"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> <xsl:element name="add"> <xsl:attribute name="key">NewSetting</xsl:attribute> <xsl:attribute name="value">New Setting Value</xsl:attribute> </xsl:element> </xsl:copy> </xsl:template> </xsl:stylesheet>
或者通过部署转换同样的事情:
Run Code Online (Sandbox Code Playgroud)<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add name="NewSetting" value="New Setting Value" xdt:Transform="Insert"/> </appSettings> </configuration>
小智 129
我发现的另一个解决方案是不使用转换,而只是有一个单独的配置文件,例如app.Release.config.然后将此行添加到csproj文件中.
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<AppConfig>App.Release.config</AppConfig>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
这不仅会生成正确的myprogram.exe.config文件,但如果您在Visual Studio中使用安装和部署项目生成MSI,它将强制部署项目在打包时使用正确的配置文件.
jer*_*enh 33
根据我的经验,我需要制作特定于环境的东西是连接字符串,appsettings和经常smpt设置.配置系统允许在单独的文件中指定这些内容.所以你可以在app.config/web.config中使用它:
<appSettings configSource="appsettings.config" />
<connectionStrings configSource="connection.config" />
<system.net>
<mailSettings>
<smtp configSource="smtp.config"/>
</mailSettings>
</system.net>
Run Code Online (Sandbox Code Playgroud)
我通常做的是将这些特定于配置的部分放在单独的文件中,在名为ConfigFiles的子文件夹中(在解决方案根目录或项目级别,取决于).我为每个配置定义一个文件,例如smtp.config.Debug和smtp.config.Release.
然后你可以定义一个预构建事件,如下所示:
copy $(ProjectDir)ConfigFiles\smtp.config.$(ConfigurationName) $(TargetDir)smtp.config
Run Code Online (Sandbox Code Playgroud)
在团队开发中,您可以通过在约定中包含%COMPUTERNAME%和/或%USERNAME%来进一步调整此值.
当然,这意味着目标文件(x.config)不应该放在源代码控制中(因为它们是生成的).您仍应将它们添加到项目文件中,并将其输出类型属性设置为"始终复制"或"如果更新则复制".
简单,可扩展,适用于所有类型的Visual Studio项目(控制台,winforms,wpf,web).
bde*_*eem 29
受Oleg和其他人在这个问题的启发,我进一步采用了解决方案/sf/answers/357667131/以启用以下功能.
此解决方案通过在MSBuild进程中首次引用app.config之前执行app.config转换来工作.它使用外部目标文件,以便更轻松地管理多个项目.
与其他解决方案类似的步骤.我引用了相同的内容并将其包含在内,以便完整性和更容易比较.
0.在项目中添加一个名为AppConfigTransformation.targets的新文件
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Transform the app config per project configuration.-->
<PropertyGroup>
<!-- This ensures compatibility across multiple versions of Visual Studio when using a solution file.
However, when using MSBuild directly you may need to override this property to 11.0 or 12.0
accordingly as part of the MSBuild script, ie /p:VisualStudioVersion=11.0;
See http://blogs.msdn.com/b/webdev/archive/2012/08/22/visual-studio-project-compatability-and-visualstudioversion.aspx -->
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.targets" />
<Target Name="SetTransformAppConfigDestination" BeforeTargets="PrepareForBuild"
Condition="exists('app.$(Configuration).config')">
<PropertyGroup>
<!-- Force build process to use the transformed configuration file from now on. -->
<AppConfig>$(IntermediateOutputPath)$(TargetFileName).config</AppConfig>
</PropertyGroup>
<Message Text="AppConfig transformation destination: = $(AppConfig)" />
</Target>
<!-- Transform the app.config after the prepare for build completes. -->
<Target Name="TransformAppConfig" AfterTargets="PrepareForBuild" Condition="exists('app.$(Configuration).config')">
<!-- Generate transformed app config in the intermediate directory -->
<TransformXml Source="app.config" Destination="$(AppConfig)" Transform="app.$(Configuration).config" />
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud)
1.为项目添加每个配置的XML文件.
通常,您将具有调试和发布配置,因此将文件命名为App.Debug.config和App.Release.config.在我的项目中,我为每种环境创建了一个配置,因此您可能想要尝试一下.
2.卸载项目并打开.csproj文件进行编辑
Visual Studio允许您在编辑器中编辑.csproj - 您只需要先卸载项目.然后右键单击它并选择Edit .csproj.
3.将App.*.配置文件绑定到主App.config
找到包含所有App.config和App.*.config引用的项目文件部分,并替换如下.您会注意到我们使用None而不是Content.
<ItemGroup>
<None Include="app.config"/>
<None Include="app.Production.config">
<DependentUpon>app.config</DependentUpon>
</None>
<None Include="app.QA.config">
<DependentUpon>app.config</DependentUpon>
</None>
<None Include="app.Development.config">
<DependentUpon>app.config</DependentUpon>
</None>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
4.激活变形魔法
在文件结束之后
Run Code Online (Sandbox Code Playgroud)<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
在决赛之前
Run Code Online (Sandbox Code Playgroud)</Project>
插入以下XML:
<Import Project="AppConfigTransformation.targets" />
Run Code Online (Sandbox Code Playgroud)
完成!
Tev*_*vin 27
您可以为每个配置使用单独的配置文件,例如app.Debug.config,app.Release.config,然后在项目文件中使用配置变量:
<PropertyGroup>
<AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
然后,这将根据您正在构建的配置创建正确的ProjectName.exe.config文件.
从 Marketplace 在 Visual Studio 中安装“配置转换工具”并重新启动 VS。您还可以看到 app.config 的菜单预览转换。
https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform
只是对现在似乎到处张贴的解决方案进行了一点改进:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
198723 次 |
最近记录: |