0

Ich habe mehrere Stunden damit verbracht, herauszufinden, warum mein benutzerdefinierter Konfigurationsabschnitt fehlschlägt, aber ich kann den Grund des Problems nicht finden.Einstellungen für den Konfigurationsabschnitt werden nicht initialisiert

Ich erhalte eine Fehlermeldung:

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.

an meinem Konfigurationsabschnitt Sehen, begann ich zu bemerken, dass ich einen String Validator aufgebaut aus:

Konfiguration CS

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

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

} 

Ich habe die String-Validator Anforderung für 1 Zeichen Länge auf afServer entfernt, und bemerkte, dass der Fehler auf afDatabase auftritt. Es scheint mir, dass die Werte niemals initialisiert werden und deshalb, wenn afServer eine Mindestlänge von 1 hat, schlägt es fehl, aber durch das Entfernen von afServer fällt der Fehler auf afDatabase.

Hier ist meine 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> 

Ich habe dies als Leitfaden wurde unter Verwendung von: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

+0

Auf welchem ​​Schritt hast du einen Fehler entdeckt? Versuchen, Wert zu erzielen, oder nur in der Laufzeit? ... etwas anderes? –

+0

Zur Laufzeit - es tritt auf, wenn ich die Anwendung ausführen. – user44036

Antwort

0

Versuchen Sie, diese

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

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

Verwandte Themen