Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Microsoft Code Contracts be used with an ASP.NET Website?

I'm currently using Microsoft Code Contracts in an ASP.NET MVC application without any issues but I can not seem to get it quite running in a basic ASP.NET Web site. I'm not entirely sure it was made to work with this type of project (although it shouldn't matter) so I wanted to bring it up to everyone.

I can compile the contracts just fine but the code skips over them since I'm assuming it hasn't been enabled through the Properties Page like you would do in other project types (ie ASP.NET MVC). I've gone to the property page of the project (which displays a dialog instead of the typical properties page) in my ASP.NET web site but it does not yield the same menu options and as such, doesn't have a section devoted to Code Contracts.

Also, I have Microsoft Code Contracts properly enabled within a class library project that I use to separate my business logic from the web site. The contracts compile fine but when a contract is violated, it throws a rather uninformative "Exception of type 'System.ExecutionEngineException' was thrown" error with no inner exception. My contract specifies a message to display upon violation but it is nowhere within the exception. It simply halts the execution of the process (which I believe is the default functionality for Microsoft Code Contracts).

I can't find anywhere that explicitly states that a particular project type can or can't (or shouldn't) be used with Contracts so I just wanted to see if anyone has had this issue.

Thanks for any help!

like image 745
jamesmillerio Avatar asked Jul 07 '09 16:07

jamesmillerio


1 Answers

I had the same problem and this is how I solved it:

In the Referenced Class Libraries, right click -> properties -> code contracts. Make sure "perform contract checking" is checked. I had mine set to "Full" Contract Reference Assembly: make sure it is set to "Build"

Save your changes.

In the Referenced Class Libraries that have no contracts in their code, set the Contract Reference Assembly to "Do Not Build".

Then in the MVC project, have the Code Contracts "perform contract checking" checked. I had mine set to "Full".

Hope that helps somebody.

like image 57
James Reategui Avatar answered Sep 25 '22 15:09

James Reategui