11

I've been developing a C# project in Visual Studio Express 2013 and came across Code Contracts for .NET languages. Impressed by their brevity and the static analysis tool that came with them, I started using them in my code base. However, when I attempted to run my program, I was met by an error message similar to the one found in this SO thread, i.e.

...An assembly (probably "<my project>") 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...

Guides suggest that to fix this, I have to enable Code Contracts from my project's Properties page, but a Code Contracts Properties entry is nowhere to be found in Express.

Some MSDN forum threads seem to indicate that all the tooling for Code Contracts is included in the Express version, but the Code Contracts Properties page is not. This seems to be the case, as I was able to run my project in VSE 2013 only after enabling Code Contracts with a copy of Visual Studio 2012 Ultimate I acquired from my university before graduating.

Is there really no way to work with Code Contracts in Visual Studio Express except by modifying the project files either manually or with a paid version of Visual Studio? If this is the case, I am extremely hesitant to use them at all, since my company is unlikely to purchase VS licenses. Futhermore, it seems extremely odd that Microsoft would attempt to proliferate this new and superior verification paradigm but then restrict it to paying customers only.

Community
  • 1
  • 1
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
  • If it is possible to enable them by modifying your project files, it's not really correct to say that it requires a paid VS license. I don't know the answer to your question, but I'm curious to hear the conclusions. – recursive Jul 14 '14 at 04:13
  • @recursive Leaving all of the libraries and tooling for Code Contracts in Express but requiring you to pay for the project properties menu entry that enables them is what baffles me. Manually editing the machine-generated project files really isn't practical - isn't that what the IDE is there for in the first place? – Matt Kline Jul 14 '14 at 04:17
  • 3
    @MattKline: I've been never used VS Express. Do you mean, that the project's property page for Code Contracts is absent in Express Edition? If so, this is really stupid. – Dennis Jul 14 '14 at 04:27
  • Yes, that is what I mean, and yes, this is really stupid. – Matt Kline Jul 14 '14 at 04:28
  • It is machine generated but my experience is that it will leave alone changes you make to it. – recursive Jul 14 '14 at 04:38
  • @recursive Sure, but there isn't documentation (AFAIK) about what tags to add or where to add them in order to enable Code Contracts. There's a reason they're usually generated by Visual Studio. – Matt Kline Jul 14 '14 at 04:41
  • VS project files are basically just MSBuild files. There is some documentation. http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx But it's a fairly deep subject, almost like learning a new language, and I can understand why some would rather not go to that length. – recursive Jul 14 '14 at 05:53
  • 2
    It is certain add-ins that are not supported and the code contracts property page is (part of) a type of add-in that is not supported in VS Express. Very unfortunate, since express is so much "lighter" to work with than a full blown VS version... – Schalk Aug 20 '14 at 12:03

3 Answers3

6

You can probably use the new VS Community 2013, as long as you meet the licensing requirements: http://www.visualstudio.com/en-us/visual-studio-community-vs

Here’s how Visual Studio Community can be used in organizations:

An unlimited number of users within an organization can use Visual Studio Community for the following scenarios: in a classroom learning environment, for academic research, or for contributing to open source projects.

For all other usage scenarios: In non-enterprise organizations, up to 5 users can use Visual Studio Community. In enterprise organizations (meaning those with >250 PCs or > $1 Million US Dollars in annual revenue), no use is permitted beyond the open source, academic research, and classroom learning environment scenarios described above.

It's basically VS 2013 Professional for free so you can install the code contracts extension.

Community
  • 1
  • 1
Nelson Rothermel
  • 9,436
  • 8
  • 62
  • 81
5

The problem is your edition of Visual Studio. You are running Visual Studio 2013 Express edition, as you stated. In order to use the binary re-writer from within Visual Studio, you need to install the Code Contracts extensions. These you can download from Microsoft's Research in Software Engineering (RiSE) site, which is packaged as a Windows Installer.

The installer installs the binary re-writer that is required, as well as a bunch of Visual Studio extensions. Unfortunately, Code Contracts states in their manual on page 40 that they don't support the various Visual Studio Express editions. You'll need to at least have Visual Studio 2013 Professional edition in order to use the Code Contracts binary re-writer extension from within Visual Studio, and in order to see the Code Contracts tab on the project properties window.

Code Contracts performs assembly re-writing as a post-build step. Meaning, Visual Studio first compiles the .NET code as it normally would. But, when using Code Contracts and enabling the right options on your project (assuming the VS extensions are installed—which you can't do in Express editions), then the binary re-writer is called for you by Visual Studio after the normal compilation process.

Instead, after compiling your program, you'll need to manually run the ccrewrite program installed by Code Contracts on the compiled assembly (and all dependent assemblies) to 're-write' your assemblies, which will add in all the Code Contract checking information. See the Code Contracts documentation (also available on the RiSE website) for information on how to do this.

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
  • 1
    You certainly can install VS extensions in Express editions, and while I'm aware that you can run `ccrewrite` manually, it just adds more pain to any coworkers in setting up their environment for this project. It's disappointing that Microsoft would hobble Code Contracts like this by only making it easy to use by paying customers. – Matt Kline Jul 31 '14 at 16:37
  • @MattKline Read the documentation for express editions. You cannot. I know; I've developed VS extensions. The least supported VS edition is Professional (NOTE: I don't know if this has changed with VS2014CTP2). It _may_ be possible to install them "manually", if you know how (it's not that hard)--but I cannot guarantee that VS would agree to load them even if they were manually copied to the correct locations on disk and the proper registry entries made. And I would argue that if you're a professional development shop developing sellable software, you should be able to afford Pro. – fourpastmidnight Jul 31 '14 at 17:49
  • 1
    @MattKline You may be able, however, to install Add-Ons. Add-Ons are distinctly different than Extensions. Perhaps this is what you were thinking of? – fourpastmidnight Jul 31 '14 at 17:51
  • 1
    I'm referring to the extensions you can install via `Tools -> Extensions and Updates`. If you're referring to those as "Add-Ons", I see no language referring to them as such in Visual Studio or in any recent Microsoft page. Furthermore, saying that my shop "should be able to afford Pro" is completely missing my point. – Matt Kline Aug 01 '14 at 21:57
  • 1
    Regardless, once code contracts were enabled in a paid version of Visual Studio (the copy of Pro 2012 I had), VS 2013 Express successfully built the project. This would imply that VS 2013 Express _has_ Code Contract support, but has hidden it from the user. – Matt Kline Aug 01 '14 at 22:00
  • @MattKline I stand corrected. You are able to create extensions for the Express editions. Furthermore, read the [Code Contracts manual](http://download.microsoft.com/download/C/2/7/C2715F76-F56C-4D37-9231-EF8076B7EC13/userdoc.pdf) next time. It tells you right in there: "the standard download allows for commercial use of the runtime contract checking tool with any Visual Studio edition **(except Express)**" [emphasis mine] on page 40. – fourpastmidnight Aug 01 '14 at 22:46
  • @MattKline If you check these few links: https://en.wikipedia.org/wiki/Microsoft_Visual_Studio_Express#Extensibility http://stackoverflow.com/questions/7940249/restrictions-on-installing-extensions-or-add-ins-in-visual-studio-2010-express http://visualstudiomagazine.com/articles/2014/05/21/no-extensions-for-visual-studio-express.aspx You'll see that no 3rd party extensions are allowed in Express editions.ence between the Express edition and the rest of the breed. – uygar.raf Sep 24 '14 at 16:07
  • @MattKline And if you check the Installation section here: http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970 You can see that the developers of Code Contracts have clearly stated that their tool cannot be installed into Express editions directly. But let's scratch that. Why on earth are you using VS2013 Express when you have a licenced VS2012? Just keep using VS2012 as long as you have the license. The extras aren't just extensions, obviously. There's a huge amount of difference between Express edition and the rest of the breed.. – uygar.raf Sep 24 '14 at 16:08
3

Contracts are supposed to be useless for Visual Studio Express 2013 (at least Code Contracts come with a plugin for all non-express versions).

So one has to define the symbol CONTRACTS_FULL in project properties -> build -> general -> conditional compilation symbols.

And then add the proper call to ccrewrite.exe to the post-build event command line found under project properties -> build events. The command should be set to something like

"C:\Program Files (x86)\Microsoft\Contracts\Bin\ccrewrite.exe" -throwOnFailure "$(TargetPath)"

I successfully used this tonight with NUnit. Using contracts, my unit tests don't need to check for post conditions explicitly.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632