Question: I'm in the process of migrating one of my projects from VS2008 to VS2010. Now that I converted all of my projects in the solution to .NET 4.0 (Client Profile) when I run the test harness, almost all tests fail with the following exception:

> 
> System.Security.VerificationException:
>  Operation could destabilize the
>  runtime.
> 
> 
> 

I've been unable to determine why this exception occurs. The tests run all fine when I run them in debug mode.

The entire solution is available for download [here](http://adamjsgenerator.codeplex.com/SourceControl/changeset/changes/0a61869498d5).

Can anyone point me in the right direction?

Answer: 
The problem seems to be related to the Code Coverage you activated. Disabling code coverage solves the issue. You could put the following in your AssemblyInfo.cs:

```
[assembly: SecurityRules(SecurityRuleSet.Level1, SkipVerificationInFullTrust = true)]

```

and reactivate code coverage.
I came across this question while looking for an answer to the a very similar problem during an upgrade from a TFS2010 to TFS2012 build server.

Our projects were already targeting .Net 4.0 and unit tests were working before the upgrade.

Since .Net 4.5 is an in-place upgrade, a .Net 4.5 bug introduced by Microsoft might cause this issue in .Net 4.0 targeted projects. It is probably related to the SecurityRules answer, but internal to framework dlls (like Microsoft.VisualStudio.QualityTools.UnitTestFramework).

For us the hotfix linked below fixed the "Operation could destabilize the runtime." exceptions in what were otherwise working unit tests.

<http://support.microsoft.com/kb/2748645>

I hope this saves somebody else ALOT of time