Question

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?

Was it helpful?

Solution

(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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top