简体   繁体   English

配置部分设置未初始化

[英]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 System.Configuration.dll中发生类型'System.Configuration.ConfigurationErrorsException'的异常,但未在用户代码中处理

Additional information: The value for the property 'afDatabase' is not valid. 附加信息:属性“ afDatabase”的值无效。 The error is: The string must be at least 1 characters long. 错误是:字符串必须至少包含1个字符。

Looking at my configuration section, I started off by noticing that I have a String Validator set up: 在查看配置部分时,我首先注意到已经设置了String Validator:

Configuration CS 配置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. 我在afServer上删除了1个字符长度的String Validator要求,并注意到该错误发生在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. 在我看来,值从未被初始化,这就是为什么当afServer的最小长度为1时,它会失败,但是通过删除它,错误将落在afDatabase上。

Here is my web.config : 这是我的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 我一直以此为指南: https : //msdn.microsoft.com/en-us/library/2tw134k3.aspx

Try this 尝试这个

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM