8

I get the following error when running Microsoft Code Contracts:

CodeContracts: Diagnostic: Failed to connect to any cache.

Contango
  • 76,540
  • 58
  • 260
  • 305

2 Answers2

10

The user manual for Code Contracts states:

Cache results: Controls if the analysis results are cached. If checked, the analysis tries to avoid analyzing methods whose outcomes cannot possibly change (because no contracts, no code, and no relevant metdata has changed). Enabling this option allows for faster turn-around times if using the static checker repeatedly. To share the cache among multiple developers, use a SQL server and put the server name in the SQL Server conguration box in the UI. Note: the SQL server connection uses Windows authentication to log onto the server. Your developers will need the right to create and modify databases.

Find out the correct name of SQL server installed on your machine (note that "localhost" will not work; it has to be the actual machine name):

enter image description here

Right click on the project, select "Properties", go to the "Code Contracts" tab (down the bottom of the list), then, add your machine name:

enter image description here

This works for Release 1.5.60911.10 (Sep 11, 2013). Perhaps future versions of Code Contracts will attempt to connect to "localhost" and/or accept "localhost" in this box, and/or automatically detect SQL Server 2012.

In my particular case, I didn't have "(localdb)\V11.0" installed; if I had installed this then it would have worked out of the box (it must be defaulting to this internally).

Crono
  • 10,211
  • 6
  • 43
  • 75
Contango
  • 76,540
  • 58
  • 260
  • 305
  • Would you happen to know what the CC engine does when the "cache results" option is set but no server is specified? – Crono Sep 25 '14 at 14:33
  • @Crono As far as I know, nothing. With oi t a database to connect to, it has no way of caching its results, so it has to regenerate it from scratch on every run. Its like omitting the SQL connection string for an app - it wont work very well. – Contango Sep 26 '14 at 16:30
  • 2
    Actually, I've found out later yesterday that it *does* create a database on its own and stores it in User/AppData/Local/CodeContracts. :) – Crono Sep 26 '14 at 19:56
  • Ah, thats brilliant! From what I understand, the advantage of a central database is so that multiple developers can share the same central database for speed improvements. – Contango Sep 26 '14 at 23:19
  • localhost works for me in Code Contracts v 1.9.10714.2. Not sure when support started. – Keith Oct 11 '16 at 02:13
3

Just uncheck the "Cache Results" option under the "Static Analysis" section of "Code Contracts" property page.

ahmkara
  • 559
  • 6
  • 6