4

Hello I have a little issue regarding CodeContracts. I have a class library project which as a class with a method foo(string s1, string s2); inside the foo method, I have a Contract.Requires(s1 != null). So if I understand the meaning of my code (yes, I just installed CodeContracts and playing with :), the contract will check the s1 != null expression during build process and in runtime, throwing ArgumentException>. I wanted to test the behavior, when I call foo(null, "test") from class lib project, the designer tells me about the issue, but when I call it from the winform app project, I don't get any warnings in error list window. So does this mean that code contracts works only in the project they reside and not outside? Thanks

UPDATE


I forgot to mention that the preconditions I have added doesn't work in static analysis. However they do throw ArgumentException with appropriate message in runtime.

Davita
  • 8,928
  • 14
  • 67
  • 119

3 Answers3

6

Ok guys, thanks to MS team, I found the problem. The reason of such a strange behavior was that my assembly's name ended with ".Contracts.dll". And the problem is that static analyzer doesn't check assemblies whose names ends with that. I renamed the assembly and everything works like a charm, just like any other MS product :)

Davita
  • 8,928
  • 14
  • 67
  • 119
  • 2
    Absolute life saver, was driving me insane! – Dave Jellison Jul 03 '11 at 23:45
  • 1
    Thank you!! They need to at least add a warning or something if this is the case. Almost 5 years after this question this issue just cost me *hours*... This is the only place on the internet I found this behavior documented. – Sabre Aug 05 '15 at 17:44
2

This should work if you have Perform Static Contract Checking checked in your winforms project. Also verify that Perform Runtime Contract Checking is checked in your class library project if you want runtime checking.

Also, in your class library project, Contract Reference Assembly should be set to Build.

From the Code Contracts documentation:

If your project contains contracts and is referenced by other projects, we strongly recommend that you select Build under the contract reference assemby section in the properties tab for CodeContracts.

This contract reference assembly is crucial to make the contracts in your project available to referencing projects. Without building a contract reference assembly, other projects cannot determine what contracts are present.

Community
  • 1
  • 1
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • Thanks for the reply andrift. Well I have. I have checked both of them in both projects, but it doesn't work. The messages window says: CodeContracts: Checked 0 assertions. any idea? – Davita Nov 27 '10 at 18:10
  • On your class library project, did you set `Contract Reference Assembly` to `Build`? – Jeff Ogata Nov 27 '10 at 19:03
  • Yes, It's set to Build in class lib project. in Winform project it's set to none – Davita Nov 27 '10 at 19:35
0

You didn't write which version of Visual Studio you're using. AFAIK Code Cotnract Static Analysis works with VS Premium only (i.e. Standard and Professional are not enough).

Registered User
  • 1,630
  • 14
  • 11