RegexStringValidatorAttribute throws ArgumentException

  • Thread starter Thread starter Sven Neumann
  • Start date Start date
S

Sven Neumann

I tried to use the RegexStringValidatorAttribute for validating a
ConfigurationProperty type of string. In some cases an ArgumentException is
thrown and I do not understand why.

I wrote a simple test to reporduces the issue.

RegexStringValidatorAttribute stringValidator = new
RegexStringValidatorAttribute("Test");

stringValidator.ValidatorInstance.Validate("Test"); // does not throw an
exception
Assert.IsTrue(stringValidator.Match("Test")); // this fails.

Any ideas?

Thanks
 
Thanks Peter,

ok, hope the following snippets make the requirement/issue more clear.

This is a sample definition of the ConfigurationPropery which I want to
validate whether the configured value does start with "TestValue".

....
[ConfigurationProperty("value")]
[RegexStringValidator("Test.*")]
public string Value {
get { return (string)this["value"]; }
set { this["value"] = value; }
}

....
The configuration snippet which sets the value-attribute to "Test1", which
matches the regular expression:

<mySection>
<myConfiguration value="Test1" />
</mySection>

Trying to access the value using the following snippet fails when trying to
get the configuration section.

MySection mySection = (MySection)ConfigurationManager.GetSection(...); //
throws ArgumentException
Assert.AreEqual("Test1", mySection.MyConfiguration.Value);

Sven
 
Short but complete...

Insert the follwing files into a UnitTest project and run TestMethod1.
You get an ArgumentException when trying to get the section, even though the
regular expression specified in the RegexStringValidatorAttribute matches the
value specified in the myConfiguration's value-attribute.

UnitTest1.cs:

using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests {
[TestClass]
public class UnitTest1 {
[TestMethod]
public void TestMethod1() {
MySection mySection =
(MySection)ConfigurationManager.GetSection("mySection");
Assert.AreEqual("Test1", mySection.MyConfiguration.Value);
}
}

public class MySection : ConfigurationSection {
[ConfigurationProperty("myConfiguration", IsRequired = true, IsKey = true)]
public MyConfigurationElement MyConfiguration {
get { return (MyConfigurationElement)this["myConfiguration"]; }
set { this["myConfiguration"] = value; }
}
}

public class MyConfigurationElement : ConfigurationElement {
[ConfigurationProperty("value")]
[RegexStringValidator("Test.*")]
public string Value {
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
}

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mySection" type="Tests.MySection, Tests" />
</configSections>
<mySection>
<myConfiguration value="Test1" />
</mySection>
</configuration>
 
Ok, next try.

1. Create a new Console Application named "ConsoleApplication1" (should be
the default).
2. Add a reference to System.Configuration.
3. Use the Program.cs code below instead of the existing one
4. Add a App.config and use the App.config code below
5. Run the console application
6. You should get a ConfigurationErrosException including an InnerException
of type ArgumentException when trying to get the section.

Program.cs:

using System.Configuration;

namespace ConsoleApplication1 {
class Program {
static void Main() {
MySection mySection =
(MySection)ConfigurationManager.GetSection("mySection");
}
}

public class MySection : ConfigurationSection {
[ConfigurationProperty("myConfiguration", IsRequired = true)]
public MyConfigurationElement MyConfiguration {
get { return (MyConfigurationElement)this["myConfiguration"]; }
set { this["myConfiguration"] = value; }
}
}

public class MyConfigurationElement : ConfigurationElement {
[ConfigurationProperty("value")]
[RegexStringValidator("Test.*")]
public string Value {
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mySection" type="ConsoleApplication1.MySection,
ConsoleApplication1" />
</configSections>
<mySection>
<myConfiguration value="Test1" />
</mySection>
</configuration>
 
Back
Top