views:

176

answers:

1

Hello all. We are migrating to .NET 4 and very interested in implementing new Design By Contract capabilities.

As we know Code Contract engine requires installation of Code Contract addin alt text
and VS Ultimate or Premium (for static checking).

Here is my questions:

  1. Can I use code contract rewriting without installing VS on CI build Server (TeamCity)?
  2. Is there any msbuild tasks to execute Contract checking?
  3. Do you use Code Contract's validation with CI builds?
+2  A: 

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

Yes. Install CodeContracts on the build server. (If it refuses to install on a machine without Visual Studio, just copy the files listed below, and their dependencies, onto the build server.) Once installed, you'll fine the CodeContract tools installed in %programfiles%\Microsoft\Contracts\Bin. In that directory, there are 4 executables you'll be interested in:

  1. ccrewrite.exe - The binary rewriter. This should be executed after compilation. It turns your contracts into runtime checks or whatever you specify you want them turned into.

  2. ccrefgen.exe - This can generate contract reference assemblies alongside your assemblies. This is useful if you're shipping dlls to be consumed by other parties.

  3. cccheck.exe - The static checker. On the build server, you'd run this tool over your assemblies containing contracts, and it will spit out warnings and messages as it encounters potential problems.

  4. ccdocgen.exe - This generates XML documentation from the contracts in your code. You might want to use this if you're shipping dlls with contracts for consumption by other parties, or if you just need internal docs on your code.

Is there any msbuild tasks to execute Contract checking?

Yes. There are 2 MSBuild tasks shipping with CodeContracts: in the same CodeContracts installation directory, check out the MSBuild\[framework version] folder. In that directory, there are 2 files that should help you out: Microsoft.CodeContracts.targets and Microsoft.CodeContractAnalysis.targets.

According to the CodeContracts documentation,

An msbuild script extension Microsoft .Contract. targets contains the extra build actions for the runtime contract instrumentation and static verication 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.

As you can see, it is possible and supported to integrate the tools into CI builds via the MSBuild targets.

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

Assuming you mean static checking with warnings/messages, I've done this personally, but haven't on a big project.

I hope this helps!

Hat tip to Jon Skeet's C# In Depth book for explanation of the command line tools.

Judah Himango
@Judah. Thanks a lot. Now we are trying to set this up in TeamCity.
Sergey Mirvoda
Cool. Please let me know the result!
Judah Himango