【问题标题】:Configuration Section Settings Not Initializing配置部分设置未初始化
【发布时间】:2016-11-29 06:00:03
【问题描述】:

我花了几个小时试图确定导致我的自定义配置部分失败的原因,但我似乎找不到问题的根源。

我收到一个错误:

System.Configuration.dll 中出现“System.Configuration.ConfigurationErrorsException”类型的异常,但未在用户代码中处理

附加信息:属性“afDatabase”的值无效。错误是:字符串长度必须至少为 1 个字符。

查看我的配置部分,我首先注意到我设置了一个字符串验证器:

配置 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;
        }
    }

}

我删除了 afServer 上 1 个字符长度的字符串验证器要求,并注意到该错误发生在 afDatabase 上。在我看来,这些值从未被初始化,这就是为什么当 afServer 的 minlength 为 1 时,它会失败,但通过删除它,错误会落在 afDatabase 上。

这是我的 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>

我一直以此为指导: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

【问题讨论】:

  • 您在哪一步出现错误?试图获得价值,或者只是在运行时?... 别的什么?
  • 在运行时 - 每当我运行应用程序时都会发生。

标签: c# asp.net-mvc visual-studio web-config configurationsection


【解决方案1】:

试试这个

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2016-12-25
    • 2020-05-31
    • 2016-11-04
    • 2012-10-05
    • 2012-11-05
    相关资源
    最近更新 更多