2

Enterprise Library 5 reads from my app.config and validates perfectly.

With the following references:

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

and the following configuration (in 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>

and the following code:

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 is, correctly, false (because "Name" is null, and I have a NotNull Validator).

However, when I replace the references to the following:

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

and change nothing else, vs.IsValid is true...

After much googling and stack overflowing, I only found this Enterprise Library 6 validation config file, (another user with a similar issue...) (*this on CodePlex)

Community
  • 1
  • 1
Novox
  • 774
  • 2
  • 7
  • 24

1 Answers1

1

Enterprise Library 6 does not automatically bootstrap the XML configuration. This is different than previous versions. So now you have to bootstrap the blocks at startup (usually).

So for validation it would look something like this:

// 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); 
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • That did the trick! However, it's not listed in the "Microsoft Enterprise Library 6 and Unity 3 Migration Guide" as a breaking change. Also, Ent Lib 6 docs state, "Enterprise Library first looks at the default configuration file (the current application domain configuration file). This will be either your App.config file (which is copied to [exe-name].config when you compile your application) or your Web.config file." So the docs seem to contradict these breaking changes, but your answer was spot on! Thanks! – Novox Oct 31 '13 at 19:55
  • While the above is still true, I did find the following: "Validation Application Block The ValidationFactory class no longer automatically builds its configuration from the configuration file. You must now invoke the SetDefaultConfigurationValidatorFactory method to load the configuration from the configuration file. This is a breaking change." at https://entlib.codeplex.com/wikipage?title=EntLib6ReleaseNotes So the docs are still contradictory. – Novox Nov 01 '13 at 14:34