【问题标题】:Class Constructor throws VerificationException after Updating to VS 2012 and .Net 4.5更新到 VS 2012 和 .Net 4.5 后,类构造函数抛出 VerificationException
【发布时间】:2012-08-12 23:34:29
【问题描述】:

我有类似的代码

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);
    }
}

由单元测试运行。在针对 .Net 4 的 VS 2010 下,单元测试运行良好。更新到 VS 2012 并针对 .Net 4.5 后,单元测试抛出

验证异常

操作可能会破坏运行时的稳定性。

异常对话框提示

确保您的应用程序没有加载两个相互冲突的类库版本。

AbstractValidator 来自 FluentValidation。正在测试的项目和单元测试项目都参考 FluentValidation 3.3.1.0。这两个项目现在也都针对 .Net 4.5。

这两个项目都以 AnyCPU 为目标。该代码在 Windows 7 64 位上运行。

更新

这是单元测试代码

[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.");
}

更新 2

可能相关

System.Security.VerificationException after installation VS 2012

但是,将配置切换到 x86 并重新运行测试会导致相同的异常

似乎不适用的类似问题

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

单元测试在没有调试器的情况下以同样的方式失败,并且将 FluentValidator 添加到 IntelliTrace 排除列表没有帮助。

Operation could destabilize the runtime?

我没有强命名程序集,也没有 AllowPartiallyTrustedCallers 属性。

更新 3

PEVerify 发现测试项目的 DLL 或正在测试的 DLL 没有问题。

【问题讨论】:

  • 哪个测试框架?哪个测试员?你可以发布你的单元测试吗?
  • 使用内置的测试框架。认为它被称为 mstest?
  • 你试过了吗:this。专门使用 PEVerify 来获取错误的更多详细信息?
  • PEVerify 表示测试的 dll 或测试项目的 dll 都没有问题。
  • 你用的是什么版本的VS2012? Intellitrace 可能会导致此问题,但仅适用于 Ultimate 版。您可以将 FluentValidation 添加为 Intellitrace 设置中的排除模块。但是,我遇到了这个确切的问题,但使用的是高级版。

标签: .net visual-studio-2012


【解决方案1】:

似乎有一个专门CLR 团队建议的修复程序

修复 .net 4.5 反射错误 将 CLR 团队建议的修复应用到 AbstractValidator 和 DelegateValidator 类型。

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

【讨论】:

    【解决方案2】:

    我今天在测试升级到 VS2012 RTM 并使用 FluentValidation 包时遇到了同样的问题。

    我们暂时使用的解决方案是将以下内容添加到 FluentValidation src 中的 'src/CommonAssemblyInfo.cs' 中并重建它:

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

    感谢讨论:http://fluentvalidation.codeplex.com/discussions/391890

    【讨论】:

    • 现在签入的 CLR 团队似乎特别建议了一个修复程序。看我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2016-08-04
    • 2013-01-01
    • 2014-11-03
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多