6

I have code like

using FluentValidation;

public class FreeformValidator : AbstractValidator<Freeform>
{
    public FreeformValidator() // <-- VerificationException on this line
    {
        RuleFor(ff => ff.Text).Must(BeLongEnough).WithMessage("Must be at least {0} characters.", ff => ff.MinLength);
    }
}

that is run by a unit test. Under VS 2010 targeting .Net 4, the unit test ran fine. After updating to VS 2012 and targeting .Net 4.5, the unit test throws

VerificationException

Operation could destabilize the runtime.

The exception dialog suggests

Make sure your application is not loading two conflicting versions of a class library.

AbstractValidator is from FluentValidation. Both the project being tested and the unit test project reference FluentValidation 3.3.1.0. Both projects also now target .Net 4.5.

Both projects target AnyCPU. The code is running on Windows 7 64-bit.

Update

Here is the unit test code

[TestMethod]
public void FreeformValidation_MinLength()
{
    Freeform fa = new Freeform();
    fa.Required = true;
    fa.MinLength = 3;
    fa.MaxLength = 10;
    FreeformValidator fv = new FreeformValidator();

    fa.Text = "AB";
    ValidationResult results = fv.Validate(fa);
    Assert.AreEqual(1, results.Errors.Count, "Expected MinLength to fail.");
    Assert.AreEqual("Must be at least 3 characters.", results.Errors[0].ErrorMessage, "Expected MinLength to fail.");
}

Update 2

Possibly related

System.Security.VerificationException after installation VS 2012

However, switching the configuration to x86 and re-running the tests results in the same Exception.

Similar issues that don't appear to apply

How can I prevent a VerificationException when running a test with attached debugger?

Unit test fails the same way without debugger, and adding FluentValidator to IntelliTrace exclusion list did not help.

Operation could destabilize the runtime?

I do not have a strongly named assembly, and no AllowPartiallyTrustedCallers attribute.

Update 3

PEVerify finds no problems with either the test project's DLL, or the DLL being tested.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Which testing framework? Which testrunner? Can you maybe post your unit test? – nemesv Aug 16 '12 at 15:29
  • Using the built-in test framework. Think it's called mstest? – Eric J. Aug 16 '12 at 15:35
  • Have you tried: [this](http://www.mattwrock.com/post/2012/02/03/VerificationException-%E2%80%9COperation-could-destabilize-the-runtime%E2%80%9D-An-exception-with-a-bark-much-worse-than-its-bite.aspx). Specifically using PEVerify to gain more detail of the error? – Andrew Clear Aug 16 '12 at 16:59
  • PEVerify says there's nothing wrong with either the tested dll or the test project's dll. – Eric J. Aug 16 '12 at 17:29
  • I wanted to mention that this also occurs in VS2010 if you installed .NET 4.5, even when you're still targeting 4.0. – Ross Sep 12 '12 at 19:04
  • What edition of VS2012 are you using? Intellitrace can cause this but only for Ultimate edition. You can add FluentValidation as an exluded module in the Intellitrace settings. However, I am having this exact issue but with Premium edition. – sroughley Aug 16 '12 at 21:13

2 Answers2

5

It looks like there's a fix specifically suggested by the CLR team:

Fixed .net 4.5 reflection bug Applied the fix, that was suggested by the CLR team, to the AbstractValidator and DelegateValidator types.

https://github.com/thecodejunkie/FluentValidation/commit/ddc1d7235b9c122c06fd224e8490b94791a715c0

Eric J.
  • 147,927
  • 63
  • 340
  • 553
3

We have had the same issue at my work today when testing an upgrade to VS2012 RTM and using the FluentValidation package.

The solution we have used for the time being is add the following to 'src/CommonAssemblyInfo.cs' in the FluentValidation src and rebuild it:

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

Credit to the discussion at: http://fluentvalidation.codeplex.com/discussions/391890

Uyllii
  • 31
  • 5
  • It looks like there is a fix specifically suggested by the CLR team checked in now. See my answer. – Eric J. Aug 17 '12 at 17:55