4

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.

Can anyone point me in the right direction?

shanethehat
  • 15,460
  • 11
  • 57
  • 87
Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
  • Can you confirm that the application is running under the CLR 4.0? Are all projects in the solution configured to target .NET 4.0? – Darin Dimitrov Oct 12 '10 at 17:31
  • Is [this](http://stackoverflow.com/questions/378895/operation-could-destabilize-the-runtime) any help? – adrianbanks Oct 12 '10 at 17:40
  • @Darin the library and its supporting application run under .NET 4 Client Profile (they don't need more than that). The test project runs under .NET 4, and it's impossible to re-target this. – Dave Van den Eynde Oct 12 '10 at 17:49
  • @adrianbanks: no, I'm not using LINQ, Expressions or delegates. – Dave Van den Eynde Oct 12 '10 at 17:49
  • @Dave, is there some stack trace associated to this exception so that you could pinpoint the exact location in your code where this is happening? Also did you update the reference of `Microsoft.VisualStudio.QualityTools.UnitTestFramework` to use `v10.0.0.0` in your unit test project? – Darin Dimitrov Oct 12 '10 at 17:56
  • @Darin no, didn't need to update that reference, it uses that version. There's not much of a stack trace, it's always in the first method call to the library under test. Tests that do not refer to the library under test succeed just fine. – Dave Van den Eynde Oct 12 '10 at 18:31
  • I added a link to where the solution can be downloaded. – Dave Van den Eynde Oct 12 '10 at 18:33

3 Answers3

7

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

First step: Run the peverify tool against the built assemblies (both test and application). It may give you some output that helps pinpoint the issue.

Second step: Can you give us the stack trace or exception output? The one time I actually saw an error with this was in .net 2 and was a compiler error - I had to slightly alter the code to make a call compile as a virtual rather than direct call. Giving us the stack and the lines of code in question would be helpful.

One thing I want to point out is that apps running under the 4.0 client profile have different security behaviors than running under 3.5. You could try adding [assembly: SecurityRules(SecurityRuleSet.Level1)] to your assemblyinfo.cs to run under the "old style" rules to help narrow it down.

Philip Rieck
  • 32,368
  • 11
  • 87
  • 99
0

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

Arkaine55
  • 548
  • 6
  • 15