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”为null,我有一个NotNull Validator)。
但是,当我替换以下引用时:
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)
答案 0 :(得分: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);