MSBuild或StyleCop任务验证App.config文件中的密钥?

时间:2012-05-16 19:40:05

标签: msbuild msbuild-task stylecop

在几个项目的App.config文件中,有一行

<add key="url" value="http://www.example.com/"/>

每次构建时,我都希望有一项任务来验证"url"密钥是否包含文本"http://localhost"。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

我假设您有一个团队,并且您的一些团队成员无意中检查了这些配置,将该值更改为localhost。

如果是这种情况,为什么不为每个环境转换文件,您的调试配置可以将密钥设置为localhost,而您的生产/测试/阶段/ qa /任何配置都可以将其设置为example.com或其他内容其他

您可能没有意识到msbuild可以转换您的配置文件。基本上,您拥有主配置文件,然后是每个环境包含仅更改内容的配置文件。在进行构建时,msbuild将使用另一个中的更改修改主要的#34;转换&#34;文件。

App.Config Transformation for projects which are not Web Projects in Visual Studio 2010?

您的转换文件如下所示:

<?xml version="1.0"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">    
  <appSettings>
    <add key="url" value="http://www.example.com/" xdt:Locator="Match(key)" xdt:Transform="SetAttributes"/>
  </appSettings>
</configuration>

Microsoft链接到http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

它们可以很容易地在web.configs和app.configs上使用,只需稍微调整一下你的项目文件。

答案 1 :(得分:0)

另一种解决方案是将单元测试作为构建的一部分进行集成,并让测试验证web.config中的密钥。

转到您的构建:

enter image description here

右键单击您的构建,然后单击编辑构建定义:

enter image description here

选择流程:

enter image description here

现在我们可以在此处设置构建失败:

enter image description here