kalittles
kalittles

Reputation: 345

Configuration Section Settings Not Initializing

I've spent several hours trying to determine what's causing my custom configuration section to fail, but I can't seem to find the root of the issue.

I'm getting an error:

An exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll but was not handled in user code

Additional information: The value for the property 'afDatabase' is not valid. The error is: The string must be at least 1 characters long.

Looking at my configuration section, I started off by noticing that I have a String Validator set up:

Configuration CS

public class TankConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("afServer", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 0, MaxLength = 60)]
    public String AfServer
    {
        get
        {
            return (String)this["afServer"];
        }
        set
        {
            this["afServer"] = value;
        }
    }

    [ConfigurationProperty("afDatabase",  IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String AfDatabase
    {
        get
        {
            return (String)this["afDatabase"];
        }
        set
        {
            this["afDatabase"] = value;
        }
    }
    [ConfigurationProperty("tankTemplate", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String TankTemplate
    {
        get
        {
            return (String)this["tankTemplate"];
        }
        set
        {
            this["tankTemplate"] = value;
        }
    }

}

I removed the String Validator requirement for 1 character length on afServer, and noticed that the error occurs on afDatabase. It seems to me that the values are never being initialized, and that's why when afServer has a minlength of 1, it fails, but by removing it, the error falls on afDatabase.

Here is my web.config:

    <configuration>

   <!-- Configuration section-handler declaration area. -->
  <configSections>
    <sectionGroup name="tankConfigurationGroup">
      <section 
        name="tankConfiguration" 
        type="TankInventory.Configurations.TankConfigurationSection"
        allowLocation="true" 
        allowDefinition="Everywhere"
      />
    </sectionGroup>
      <!-- Other <section> and <sectionGroup> elements. -->
  </configSections>

  <!-- Configuration section settings area. -->
<tankConfigurationGroup>
          <tankConfiguration afServer ="Test123" afDatabase ="Test123" tankTemplate ="Test21345" >
        </tankConfiguration>
      </tankConfigurationGroup>
</configuration>

I've been using this as a guide: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

Upvotes: 0

Views: 336

Answers (1)

Emiliano Barboza
Emiliano Barboza

Reputation: 485

Try this

<configSections>    
  <section name="tankConfiguration" type="TankInventory.Configurations.TankConfigurationSection"
    allowLocation="true" 
    allowDefinition="Everywhere"
  />

      <tankConfiguration afServer ="Test123" afDatabase ="Test123" tankTemplate ="Test21345"/>>

Upvotes: 0

Related Questions