2

I have some simple WebAPI Models/Repositories/Controllers.

I was able to test these Friday in a browser using:

http://localhost:48614/api/departments/

...which would return the test departments data in the browser page.

Now, though, the same attempt fails with this:

enter image description here

Is this really so? I need to jump through these hoops, when last week my simple test ran fine?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Did you add in some code contracts stuff? –  Oct 14 '13 at 17:26
  • Yes, I did, like so: Contract.Requires(item != null, "item"); The fact that you ask the question makes me think this is a problem. Is it easily solved (other than commenting out all such code)? – B. Clay Shannon-B. Crow Raven Oct 14 '13 at 17:38
  • I guess the err msg, which at first seemed cryptic, is pretty plain, but is it really so that using code contracts requires all this rigamarole? – B. Clay Shannon-B. Crow Raven Oct 14 '13 at 17:42
  • Yup. Don't forget that ASP.NET compiles for you outside of visual studio. So, if you have code contracts in your code, you need them to be transformed as they would via a normal build. Simple facts. At least you'll see what magic is being performed on your behalf by doing all this stuff manually. –  Oct 14 '13 at 17:49
  • Okay; make that an answer, and I'll mark it as such. I must say, though, that Code Contracts have lost a good part of their sheen to me due to this (pain in the) asterisk. – B. Clay Shannon-B. Crow Raven Oct 14 '13 at 18:14

2 Answers2

3

(Please note, I'm not an expert on CC, but I play one on StackOverflow. Some of these statements may be slightly incorrect)
Code Contracts aren't just a collection of methods in an assembly you reference and call in order to perform validation. Code Contracts are a set of types and tooling that actually rewrites your IL after compilation to perform static and runtime checks. You can read more about this rewriter here.

They are still not an official part of the framework or of Visual Studio. You have to download and install the tooling for visual studio before it works.

Editable ASP.NET websites perform the compilation of aspx/cshtml files on first load. As Code Contracts require your compiled assemblies be rewritten, this would have to be included in the ASP.NET compilation stage. I'm not sure how to do that (I'd visit the forums for that info), but it definitely won't happen by magic.

The error suggests that compilation is not being performed correctly either in VS or by ASP.NET. Either way, the only way to fix it is to ditch CCs altogether, or do what the dialog says.

I'll note that Daniel Cazzulino has some CC-like code available via nuget that includes many helper methods that behave similar to Code Contracts. Things like Assert.NotNull(someArgument, "someArgument");. You can find them if you search for his account.

2

Use the non-generic version of Contract.Requires so the IL rewriting is not required. The documentation touches on this.

Dandy
  • 272
  • 3
  • 11