VAB: not working with config file other than web.config

Topics: Validation Application Block
May 24, 2007 at 4:33 PM
What am i doing wrong?

In the same folder as the web.config I copied the web.config file to test.config and used the following code:

Validator<Facility> FacilityValidator = ValidationFactory.CreateValidatorFromConfiguration<Facility>("RuleSetA",new FileConfigurationSource("test.config"));
_ValidationResults = FacilityValidator.Validate(facility);

The code above doesn't evaluate any validators and _ValidationResults.isvalid == true with no exceptions thrown.

Then I try the following and it works:

Validator<Facility> FacilityValidator = ValidationFactory.CreateValidatorFromConfiguration<Facility>("RuleSetA");
_ValidationResults = FacilityValidator.Validate(facility);

For some reason the web.config file works but an external config file doesn't even if it's identical to the web.config. I did step through the code enough to see that the path on the FileConfigurationSource object is correct, but didn't step into it further to see what the issue was when loading the validators from the Test.config.

I believe this is a bug, please let me know.


May 24, 2007 at 4:35 PM
In addition to this there is an object collection validator. Maybe this is the issue I believe there is a work item already created, but I would appreciate a confirmation on this...thanks.
May 25, 2007 at 6:39 PM
Make any headway on this issue? I believe that I'm going through the same issue in that when rules exist in the web.config file they are validated...when they are moved to another config file they are not.

After moving the rules to the 'new' file the web.config was modified to have a section to define the new configuration file, using the File Configuration Source tag (specifies physical file path). The new XML file has a single section titled "validation" and then includes the identical rules that were previously in the web.config.

You following the similar process to have it fail?

Anyone have thoughts?

May 28, 2007 at 2:50 PM
Sorry, no developments with this, I'm just using the web.config until it gets fixed.

Maybe someone on the EntLib team can confirm that there is an issue here?
May 28, 2007 at 3:27 PM
Hey there,

By following the c# quickstart ASP.NET application I managed to get it working using an external config file. I have placed sample code below. The couple of things that I am working on now is that the section defined in the web.config appears to HAVE to be named "enterpriseLibrary.ConfigurationSource".

The other issue is that from what I have tested and played with it appears that only one external file can be used to store validation rules. It seems that the VAB is looking for the rules to be defined in the "enterpriseLibrary.ConfigurationSource" section and the physical location of the file is under the "File Configuration Source" attribute. To simplify the maintenance on our objects, we would like to have one VAB XML file for each business object. I would like to know if someone else has completed this before?


Sample code:


<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=, Culture=neutral, PublicKeyToken=null"
filePath="C:\Temp\TestExternal.config" />


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

<type assemblyName="Globals.Business, Version=, Culture=neutral, PublicKeyToken=null"
<ruleset name="notificationConfig">
<property name="Name">
<validator messageTemplate="" MessageTemplateResourceName=""
messageTemplateResourceType="" tag=""
type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=null"
name="And Composite Validator" />

<validator negated="false" messageTemplate="from external.configName cannot be empty."
messageTemplateResourceName="" messageTemplateResourceType=""
tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=null"
name="Not Null Validator" />

<validator lowerBound="1" lowerBoundType="Inclusive" upperBound="25" upperBoundType="Inclusive" negated="false"
messageTemplate="from external.configName must be between 1 and 25 characters long."
messageTemplateResourceName="" messageTemplateResourceType="" tag=""
type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=null"
name="String Length Validator" />
May 28, 2007 at 5:42 PM
Validation rules can be placed in any IConfigurationSource.

This question has already been answered in another thread. If you ever get stuck on a problem, please remember that you can search the forums for past questions and possibly find a solution before posting the question again.

Placing rules in one config file per class




David Hayden
Microsoft MVP C#
May 28, 2007 at 5:58 PM

There is no bug to fix. What you are trying to do works fine. There is a bug somewhere in your code/configuration.

I duplicated the same thing just now:

Customer customer = new Customer();
Validator < Customer > customerValidator = ValidationFactory.CreateValidatorFromConfiguration<Customer>("RuleSetA", new FileConfigurationSource("CustomerRules.config"));
ValidationResults results = customerValidator.Validate(customer);
if (!results.IsValid)
     // ...


<?xml version="1.0" encoding="utf-8"?>
    <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <type assemblyName="ConsoleApplication2, Version=, Culture=neutral, PublicKeyToken=null"
      <ruleset name="RuleSetA">
          <field name="Email">
            <validator negated="false" messageTemplate="Cannot be null."
              messageTemplateResourceName="" messageTemplateResourceType=""
              tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
              name="Not Null Validator" />




David Hayden
Microsoft MVP C#
May 28, 2007 at 6:17 PM
Hi there.

Thanks for the response. Yes I had previously found that article. You are correct that completing the logic outlined in that document does work. This of course is providing that you are using the ValidationFactory.CreateValidator syntax.

What we are trying to do is use the "PropertyProxyValidator" ASP.NET web control to refer to the external configuration files. This is so that the Page.IsValid property fails and will log the appropriate messages. By changing the "SpecificationSource" of the control to "Configuration" we are able to validate an external file by modifying the web.config file to have a section named "enterpriseLibrary.ConfigurationSource",

By having one external XML file identiifed in the "enterpriseLibrary.ConfigurationSource" detail section the Page.IsValid section checks the errors identified in the external config file. Are we able to configure multiple XML files in the web.config file so that the PropertyProxyValidator control can be modified to scan the multiple file names for the assembly and type values?

May 28, 2007 at 6:50 PM

I see. That brings up a great point. I don't know of a way to do that and it is a legitimate problem IMHO.

I have asked one of the developers if there is a solution for this. I will let you know when I get an answer.




David Hayden
Microsoft MVP C#
May 28, 2007 at 9:15 PM
The response was that this is a known limitation at this time. It is not technically impossible. They just didn't have enough time to add the functionality in the initial release.

I recommend adding it as a WorkItem so it has a chance of being added in the future.




David Hayden
Microsoft MVP C#
May 28, 2007 at 9:53 PM
Thanks for all of the information.

I will be posting a Work Item on this topic.

May 28, 2007 at 10:25 PM
Thanks TJ,

Those file configuration settings, that point to a configuration file, were missing in my web.config. Missed that when reviewing the quick start app.

Thanks again for the tip!