我的持续集成服务器 (TeamCity) 配置为在构建时运行我们应用程序中的所有单元测试。在运行这些测试之前,我需要更改一些 appSettings 以使它们对我们的 CI 服务器有效。通过使用 Visual Studio 提供的部署项目,我正在为我的 Web 项目实现类似的功能。我可以为测试项目做同样的事情吗?
谢谢,贡萨洛
最佳答案
可以通过变通方法对 App.config 文件使用 Web.config 转换。
您只需在构建过程的正确阶段调用适当的 MSBuild 任务。
将此代码片段添加到您的项目文件中:
<UsingTask
TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('App.$(Configuration).config')">
<!-- Generates the transformed App.config in the intermediate directory -->
<TransformXml
Source="App.config"
Destination="$(IntermediateOutputPath)$(TargetFileName).config"
Transform="App.$(Configuration).config" />
<!-- Forces the build process to use the transformed configuration file -->
<ItemGroup>
<AppConfigWithTargetPath Remove="App.config"/>
<AppConfigWithTargetPath
Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
然后为您希望应用转换的每个构建配置添加额外的 App.config 文件到您的项目。例如:
<ItemGroup>
<None Include="App.config" />
<None Include="App.Release.config">
<DependentUpon>App.config</DependentUpon>
</None>
</ItemGroup>
相关资源:
关于c# - 单元测试的 App.config 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5091243/