Whilst changing the Entity Framework 5.0 NuGet package to work with SharpDevelop a custom build of the NuGet package was causing Visual Studio 2010 to crash when the package was being installed. Debugging the crash by attaching another instance of Visual Studio revealed that an ExecutionEngineException was being thrown when the following line of code was being executed.
Contract.Requires(project != null);
The exception itself had no other information. The exception’s message just repeated the exception that was being thrown: “Exception of type ‘System.ExecutionEngineException’ was thrown.”. This exception indicates an internal error occurred in the CLR execution engine.
The real problem is the use of Code Contracts and not following the Entity Framework documentation on how to build it from source code.
The Contract class is used to define required preconditions and postconditions for a method and is used when defining Code Contracts. It is part of the .NET Framework 4.0 and can be found in mscorlib.dll in the System.Diagnostics.Contracts namespace.
The problem with the custom build of Entity Framework is that there are parts of Code Contracts included with .NET Framework 4.0 so you can compile a project that uses Code Contracts without any errors but on running the application you can have an ExecutionEngineException thrown. This can occur if you do not have the Code Contracts Visual Studio extension installed when the project is built. With this extension installed your assembly will be modified when it is compiled by the code contracts binary rewriter. The rewriter will inject the contracts into your assembly so the checks will be run when the code is executed.
To get around this problem you have two options.
Install the Code Contracts Visual Studio extension and then rebuild your project. After installing you should have a Code Contracts page in your project options.
Remove CONTRACTS_FULL from the conditional compilation symbols in the project’s options.
CONTRACTS_FULL is used to enable runtime checking of the code contracts. If this is not defined then the calls made to the any of Contract methods are ignored by the compiler when building your project.