11
Vote

ValidationFailure constructors cause System.Security.VerificationException in unit tests being run with code coverage

description

When running any unit test under a code coverage tool (e.g. TestDriven.NET "Test With" NCover or VS Coverage) which contains a call to either of the ValidationResult constructors, I get a System.Security.VerificationException like

Test 'xxx' failed: System.Security.VerificationException : Operation could destabilize the runtime. at FluentValidation.Results.ValidationResult..ctor()

Based on this SO answer: http://stackoverflow.com/a/2883384/236255, it looks like removing the AllowPartiallyTrustedCallers assembly attribute might be the fix in http://fluentvalidation.codeplex.com/SourceControl/changeset/view/58a381d61018#src%2fCommonAssemblyInfo.cs

Thanks.

comments

jbfurlong wrote Feb 29, 2012 at 10:54 AM

I get a similar issue when running under Debug. Without Debug on a simple NotEmpty() rule my tests work. As soon as I debug the test then the ForRule throws a System.Security.VerificationException with the following stacktrace:

at FluentValidation.Resources.LocalizedStringSource.CreateFromExpression(Expression1 expression, IResourceAccessorBuilder resourceProviderSelectionStrategy)
at FluentValidation.Validators.PropertyValidator..ctor(Expression
1 errorMessageResourceSelector)
at FluentValidation.Validators.NotEmptyValidator..ctor(Object defaultValueForType)
at FluentValidation.DefaultValidatorExtensions.NotEmpty[T,TProperty](IRuleBuilder`2 ruleBuilder)
at EST.MW.Common.Validation.ContactUsValidator..ctor()

jbfurlong wrote Feb 29, 2012 at 10:58 AM

Specter75 wrote Aug 16, 2012 at 9:28 AM

I have the same problem but running an asp.net mvc project.

ericjohannsen wrote Aug 16, 2012 at 3:30 PM

I'm also getting this issue. Details at

http://stackoverflow.com/q/11990208/141172

ericjohannsen wrote Aug 16, 2012 at 8:12 PM

Additional information to previous comment:

I get this in Visual Studio 2012 targeting .Net 4.5 running the test (not debugging, no code coverage). The other linked issue on Stack Overflow did not resolve this for me.

See

http://stackoverflow.com/q/11990208/141172

stephenroughley wrote Aug 16, 2012 at 9:26 PM

Exact same issue here. Visual Studio 2012 Premium, FluentValidation 3.2. VerificationException thrown by .ctor of Validator.

exyll wrote Aug 17, 2012 at 7:24 AM

I'm also getting a similar exception:
Operation could destabilize the runtime.
System.Security.VerificationException: Operation could destabilize the runtime.
at FluentValidation.AbstractValidator`1..ctor()

ITmeze wrote Aug 17, 2012 at 7:28 AM

I am getting this after upgrading to .NET 4.5 from 4.5 RC:

Operation could destabilize the runtime.
System.Security.VerificationException: Operation could destabilize the runtime.
at FluentValidation.AbstractValidator`1..ctor()

I am using Autofac, It was fine till i was using .NET 4.5 RC

Lion_cl wrote Mar 13, 2013 at 5:45 AM

You should remove this line
[assembly: AllowPartiallyTrustedCallers]
from CommonAssemblyInfo.cs to prevent this error.