Peter K.
Peter K.

Reputation: 8108

Code Contracts Runtime Checking Exception

We use Microsoft's code contracts in our codebase.

A project had Contract.Requires statements in it, without the build configuration that is released having "Perform Runtime Contract Checking" checkbox being ticked.

When the release build hits the Contract.Requires statement, an exception is generated. This eventually trickles up and causes a System.ServiceModel.CommunicationException in the web service that uses the code contracts.

There was no indication anywhere in the exception** that this was a code contracts issue.

Is there any way to expose the fact that it's a code contract issue?

The issue was resolved by ticking the "Perform Runtime Contract Checking" checkbox for the release build configuration, but it would be good to ALSO have better diagnostic information available at runtime.

enter image description here

** Still working on digging into the inner exceptions.

Upvotes: 2

Views: 427

Answers (1)

porges
porges

Reputation: 30580

The rewriter is always needed, even if you don't want runtime checking. The name of that option is misleading! If you want to turn off runtime checking, you need to enable that checkbox, but set the dropdown to "none".

If you don't have the rewriter enabled, the code will just call the actual methods in the Contracts class, which are really just dummy methods for the rewriter to access. If you actually call them, they just throw an exception.

Upvotes: 4

Related Questions