Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Constructor throws VerificationException after Updating to VS 2012 and .Net 4.5

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.

like image 845
Eric J. Avatar asked Aug 16 '12 15:08

Eric J.


2 Answers

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

like image 163
Eric J. Avatar answered Oct 16 '22 14:10

Eric J.


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

like image 34
Uyllii Avatar answered Oct 16 '22 16:10

Uyllii