微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 为3种不同的环境转换app.config

我需要能够使用msbuild转换我的app.config文件.我可以转换文件,如果它被称为app.DEBUG.config或app.Release.config,但我不能添加一个名为app.PROD.config文件.

使用常规XDT转换如果我选择不同的PublishProfile,msbuild会识别不同的web.config文件

msbuild path.to.project.csproj Configuration=Release PublishProfile=DEV

显然app.config不能使用相同的设置.我总是可以为DEV.config设置创建一个特定的构建配置,但对一个应用程序进行单独的构建确认似乎没用.一个hacky方法是只为每个环境POST-BUILD复制正确的app.config.

我试图使用SlowCheetah插件,但这似乎只是转换认的DEBUG和RELEASE配置文件,这是不合适的,因为我有两个以上的环境.如果我确实使用了这个错误,请让我知道我应该将哪个参数传递给msbuild来选择我的app.DEV.config.

预期的结果是msbuild将根据自定义的变换app.DEV.config或为app.PROD.config自定义的变换来变换app.config.我希望有一个参数可以传递给msbuild,这将允许我使用Release配置,但每个环境使用不同名称的转换.

解决方法

这是我在这种情况下使用的:
<?xml version="1.0" encoding="utf-8"?>
<Project Toolsversion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- This target will run right before you run your app in Visual Studio -->
  <Target Name="UpdateWebConfigBeforeRun" BeforeTargets="Build">
    <Message Text="Configuration: $(Configuration) update from web.template.$(Configuration).config"/>
    <TransformXml Source="web.template.config"
              Transform="web.template.$(Configuration).config"
              Destination="web.config" />
    </Target>

  <!-- Exclude the config template files from the created package -->
  <Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="web.template.config;web.template.*.config"/>
    </ItemGroup>
    <Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
  </Target>
</Project>

我有以下设置:

web.template.config
    - web.template.debug.config
    - web.template.production.config
    - web.template.release.config etc

应该在不需要额外插件的情况下工作.在你的场景中,你需要编辑内容来说app.而不是网络.

原文地址:https://www.jb51.cc/csharp/96804.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐