1

I have just started using code contracts to make my preconditions neater and more readable, hoping to get some further benefits like static checking later. However as I committed my code and a build was done it failed on the unit tests (that test my code which uses CC) with the error:

... threw exception: System.Diagnostics.Contracts.ContractException: An assembly (probably "...") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires and the CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild. CCRewrite can be downloaded from http://go.microsoft.com/fwlink/?LinkID=169180. After the rewriter is installed, it can be enabled in Visual Studio from the project's Properties page on the Code Contracts pane. Ensure that "Perform Runtime Contract Checking" is enabled, which will define CONTRACTS_FULL.

It is a TFS 2010 Build Server, and it has not had anything extra installed for Code Contracts. I installed the CC msi locally and do not have any problem. My CC properties page for the project is set up as follows:

enter image description here

I have googled and it seems many people are complaining that TFS does not have support for CC. I do not currently have admin rights to the build box but I am getting it to try and install the same msi there. Does anyone know what the resolution to this problem is?

sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • have a look at this question http://stackoverflow.com/questions/25581762/code-contracts-tfs-online-build-server-unit-tests-fail – NeddySpaghetti Dec 24 '14 at 10:22
  • @NedStoyanov yes I have already read that, the answer is not clear it says "use of Contract.Requires instead of Contract.Requires" which makes no sense. Also if it means to get rid of the version which throws an exception then I cannot do that as I need to throw an exception as my unit tests expect that. – sprocket12 Dec 24 '14 at 12:48
  • There's also http://blog.mycupof.net/2013/03/30/team-foundation-service-make-codecontracts-work-on-a-hosted-build-agent/, rather complicated and http://stackoverflow.com/questions/4291254/microsoft-code-contracts-without-visual-studio – stijn Dec 25 '14 at 08:44

2 Answers2

2

I used Reflector to have a look a the definition of Contract.Requires and I think that the problem occurs when your code uses an overload of the method that does not have the attribute Conditional("CONTRACTS_FULL") applied to it. That means the code is compiled on your TFS server, which doesn't have code contracts installed and you get the error you mentioned.

I'd try changing the code to use one of the overloads that has the Conditional("CONTRACTS_FULL") attribute applied to it.

    [Conditional("CONTRACTS_FULL"), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), __DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public static void Requires(bool condition);
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), __DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public static void Requires<TException>(bool condition) where TException: Exception;
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), Conditional("CONTRACTS_FULL"), __DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public static void Requires(bool condition, string userMessage);
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), __DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public static void Requires<TException>(bool condition, string userMessage) where TException: Exception;
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
0

Please see the Code Contracts manual. It definitely tells you all you need to know about using Code Contracts with unit tests and automated CI builds.

Specifically, please read Section 7.8: Test Harness Setup. Also see Section 6.2: Runtime Contract Checking. It tells you about the various requirments when checking contracts and suggests having build types specifically for performing contract checking (which you may not want to perform as part of your CI build--you might, but then again, you might not; so read this section). For example, they suggest you may want to have a Checked build that performs contract checking, in addition to standard Debug and Release builds. You'll probably also want to look at all of Section 5: Usage Guidelines, as much of this information will need to be understood before understanding all that is written in Section 6: Visual Studio Integration, for which much of that information is applicable to build servers (since they build your code).

Also, from the error and screenshot you posted, it appears that you are using the option Standard Contract Requires. If so, then you are required to use the binary rewriter ccrewrite when building your code. So your build server will need to have Code Contracts installed. You can learn more about this in Section 6.1 Assembly Mode.

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48