c# - Microsoft Code Contracts 和 CI 构建服务器

标签 c# .net continuous-integration teamcity code-contracts

我们正在迁移到 .NET 4,并且对实现新的按契约(Contract)设计功能非常感兴趣。

据我们所知Code Contract引擎需要安装 Code Contract addin alt text
和 VS Ultimate 或 Premium(用于静态检查)。

这是我的问题:

  1. 我可以使用代码契约重写吗 没有在 CI 构建服务器 (TeamCity) 上安装 VS?
  2. 有没有 msbuild 任务来执行契约(Contract)检查?
  3. 您是否将 Code Contract 的验证用于 CI 构建?

最佳答案

Can I use code contract rewriting without installing VS on CI build server (TeamCity)?

是的。在构建服务器上安装 CodeContracts。 (如果它拒绝在没有 Visual Studio 的机器上安装,只需将下面列出的文件及其依赖项复制到构建服务器上。)安装后,您会发现 CodeContract 工具安装在 %programfiles%\Microsoft\Contracts\垃圾桶。在该目录中,有 4 个您会感兴趣的可执行文件:

  1. ccrewrite.exe - 二进制重写器。这应该在编译后执行。它将您的契约(Contract)变成运行时检查或您指定的任何您希望它们变成的内容。

  2. ccrefgen.exe - 这可以在您的程序集旁边生成契约(Contract)引用程序集。如果您运送 dll 供其他方使用,这将非常有用。

  3. cccheck.exe - 静态检查器。在构建服务器上,您将在包含契约(Contract)的程序集上运行此工具,它会在遇到潜在问题时发出警告和消息。

  4. ccdocgen.exe - 这会根据代码中的契约(Contract)生成 XML 文档。如果您要运送带有契约(Contract)的 dll 以供其他方使用,或者如果您只需要代码的内部文档,则可能需要使用它。

Is there any msbuild tasks to execute Contract checking?

是的。 CodeContracts 附带了 2 个 MSBuild 任务:在同一个 CodeContracts 安装目录中,查看 MSBuild\[framework version] 文件夹。在该目录中,有 2 个文件可以帮助您:Microsoft.CodeContracts.targets 和 Microsoft.CodeContractAnalysis.targets。

根据 CodeContracts 文档,

An msbuild script extension Microsoft .Contract. targets contains the extra build actions for the runtime contract instrumentation and static verification steps. As a result of this approach, it is possible to use the same functionality when building from the command line with the msbuild command. Using msbuild on a project or solution that uses contracts enabled via the VS user interface will perform the same actions as the corresponding build under VS.

如您所见,可以并支持通过 MSBuild 目标将工具集成到 CI 构建中。

Do you use Code Contract's validation with CI builds?

假设您的意思是使用警告/消息进行静态检查,我个人已经这样做过,但还没有在大项目中这样做过。

希望对您有所帮助!

向 Jon Skeet 的 C# In Depth 致敬解释命令行工具的书。

关于c# - Microsoft Code Contracts 和 CI 构建服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3569108/

相关文章:

c# - 我无法用局部变量 C# 初始化内部结构

c# - C#如何计算没有括号的AND OR表达式

c# - 在生成服务器上编译异步时无效 token 'void'..await

java - 我的 Java 项目是否有任何自动指标收集器?

.net - CI : Hudson with . 网络与 CruiseControl.Net

c# - 如何在 Microsoft bot 框架中设置 "suggested actions"的样式?

.net - 如何为我的公司创建一个通用的整体 .NET 编程结构?

c# - 运算符 '+' 不能应用于类型 'byte[]' 和 'int' 的操作数

.net - CruiseControl.NET 持续集成阻止文件访问

ios - 具有持续集成功能的 FacebookSDK - Xcode