CodeContracts issue

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.


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 :)


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.


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

链接地址: http://www.djcxy.com/p/23738.html

上一篇: 为什么CodeContracts静态分析器不会发出警告?

下一篇: CodeContracts问题