【问题标题】:Enterprise Library 6 Validation Not Reading From Configuration?Enterprise Library 6 验证未从配置中读取?
【发布时间】:2013-11-11 18:20:32
【问题描述】:

Enterprise Library 5 从我的 app.config 中读取并完美验证。

有以下参考:

Microsoft.Practices.EnterpriseLibrary.Common v 5.0.414.0 Microsoft.Practices.EnterpriseLibrary.Validation v 5.0.414.0

以及以下配置(在 app.config 中):

<configSections>
<section name="validation"
         type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings,
             Microsoft.Practices.EnterpriseLibrary.Validation" />
</configSections>

<validation>
<type name="WindowsFormsApplication1.AThing" assemblyName="WindowsFormsApplication1" defaultRuleset="default">
  <ruleset name="default">
    <properties>
      <property name="Name">            
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation"
          negated="false" messageTemplate="Customer must have valid no"
          tag="CustomerNo" name="Not Null Validator" />
      </property>
    </properties>
  </ruleset>
</type>
</validation>

以及以下代码:

public class AThing
{
    public string Name { get; set; }
}

...
AThing bob = new AThing();
bob.Name = null;
ValidationResults vr = Validation.Validate(bob, "default");
Debug.Assert(!vr.IsValid); 
...

vr.IsValid 正确地是 false(因为“Name”为空,而且我有一个 NotNull 验证器)。

但是,当我替换对以下内容的引用时:

Microsoft.Practices.EnterpriseLibrary.Common v 6.0.0.0 Microsoft.Practices.EnterpriseLibrary.Validation v 6.0.0.0

并且什么都不改变,vs.IsValid是true...

经过多次谷歌搜索和堆栈溢出,我只找到了这个Enterprise Library 6 validation config file,(另一个有类似问题的用户......)(*this on CodePlex

【问题讨论】:

    标签: c# validation enterprise-library enterprise-library-5 enterprise-library-6


    【解决方案1】:

    Enterprise Library 6 不会自动引导 XML 配置。这与以前的版本不同。所以现在你必须在启动时引导块(通常)。

    所以对于验证,它看起来像这样:

    // Bootstrap the block at startup using default configuration file
    ValidationFactory.SetDefaultConfigurationValidatorFactory(
        new SystemConfigurationSource());
    
    AThing bob = new AThing();
    bob.Name = null;
    
    ValidationResults vr = Validation.Validate(bob, "default");
    Debug.Assert(!vr.IsValid); 
    

    【讨论】:

    • 成功了!但是,“Microsoft Enterprise Library 6 和 Unity 3 迁移指南”中并未将其列为重大更改。此外,Ent Lib 6 文档状态,“企业库首先查看默认配置文件(当前应用程序域配置文件)。这将是您的 App.config 文件(当您复制到 [exe-name].config编译您的应用程序)或您的 Web.config 文件。”所以文档似乎与这些重大变化相矛盾,但你的答案是正确的!谢谢!
    • 虽然上述情况仍然正确,但我确实发现了以下内容:“验证应用程序块 ValidationFactory 类不再自动从配置文件构建其配置。您现在必须调用 SetDefaultConfigurationValidatorFactory 方法来加载配置来自配置文件。这是一个重大更改。”在entlib.codeplex.com/wikipage?title=EntLib6ReleaseNotes 所以文档仍然是矛盾的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多