ConfigurationSection - StringValidator失败 [英] ConfigurationSection - StringValidator failing

查看:106
本文介绍了ConfigurationSection - StringValidator失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自定义元素中的ASP.NET web.config文件中存储三个值,因此这是我的web.config:

 < code><?xml version =1.0?> 
< configuration>
< configSections>
< section name =mySectiontype =Foobar.MySection,Foobar/>
< / configSections>

< mySection baseUri =https:// blarghsid =123key =abc/>

<! - etc,包括< system.web>配置 - >
< / config>

这是我的配置代码:

 命名空间Foobar {

public class MySection:ConfigurationSection {

public MySection(){
}

[ConfigurationProperty(baseUri,IsRequired = true)]
[StringValidator(MinLength = 1)]
public String BaseUri {
get {return(String)this [baseUri] ; }
set {this [baseUri] = value; }
}

[ConfigurationProperty(sid,IsRequired = true)]
[StringValidator(MinLength = 1)]
public String Sid {
get {return(String)this [sid]; }
set {this [sid] = value; }
}

[ConfigurationProperty(key,IsRequired = true)]
[StringValidator(MinLength = 1)]
public String Key {
get {return(String)this [key]; }
set {this [key] = value; }
}
}
}

此代码:

  MySection section = ConfigurationManager.GetSection(mySection)as MySection; 
String x = section.BaseUri;但是当我在ASP.NET中运行我的代码时,我得到这个异常:

$ b


$ b

  [ArgumentException:字符串长度必须至少为1个字符。] 
System.Configuration.StringValidator.Validate(Object value)+679298
System.Configuration.ConfigurationProperty.Validate(对象值)+41

[ConfigurationErrorsException:属性'baseUri'的值无效。错误是:字符串必须至少为1个字符长。]
System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentConfig,ConfigXmlReader reader,String filename,Int32 line)+278
System.Configuration.BaseConfigurationRecord.CreateSectionDefault(String configKey,Boolean getRuntimeObject,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object& result,Object& resultRuntimeObject)+59
System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object& result,Object& resultRuntimeObject)+1431
System.Configuration.BaseConfigurationRecord.GetSection(String configKey,Boolean getLkg,Boolean checkPermission)+56
系统。 Configuration.BaseConfigurationRecord.GetSection(String configKey)+8
System.Web.HttpContext.GetSection(String sectionName)+47
System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName)+39
System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey)+6
System.Configuration.ConfigurationManager.GetSection(String sectionName)+78
< my code that调用GetSection>

为什么在web.config中设置正确格式的值时StringValidator会失败?

因为 StringValidator 是我忽略的原因,所以我忽略了什么?

解决方案

异常我做了一些googling,显然是一个在.NET框架中的错误:有一个 StringValidator 与最小长度参数意味着它将总是拒绝空字符串属性的默认值,将由框架设置。



有解决方法,花费时间在他们,所以我剥离了 StringValidator 属性,我的代码工作正常。



这里的QA我发现:为什么StringValidator对于自定义配置部分总是失败?


I want to store three values in my ASP.NET web.config file in a custom element, so this is my web.config:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="mySection" type="Foobar.MySection,Foobar" />
    </configSections>

    <mySection baseUri="https://blargh" sid="123" key="abc" />

    <!-- etc, including <system.web> configuration -->
</configuration>

This is my Configuration code:

namespace Foobar {

public class MySection : ConfigurationSection {

    public MySection () {
    }

    [ConfigurationProperty("baseUri", IsRequired=true)]
    [StringValidator(MinLength=1)]
    public String BaseUri {
        get { return (String)this["baseUri"]; }
        set { this["baseUri"] = value; }
    }

    [ConfigurationProperty("sid", IsRequired=true)]
    [StringValidator(MinLength=1)]
    public String Sid {
        get { return (String)this["sid"]; }
        set { this["sid"] = value; }
    }

    [ConfigurationProperty("key", IsRequired=true)]
    [StringValidator(MinLength=1)]
    public String Key {
        get { return (String)this["key"]; }
        set { this["key"] = value; }
    }
}
}

And I load it using this code:

MySection section = ConfigurationManager.GetSection("mySection") as MySection;
String x = section.BaseUri;

However when I run my code in ASP.NET I get this exception:

[ArgumentException: The string must be at least 1 characters long.]
System.Configuration.StringValidator.Validate(Object value) +679298
System.Configuration.ConfigurationProperty.Validate(Object value) +41

[ConfigurationErrorsException: The value for the property 'baseUri' is not valid. The error is: The string must be at least 1 characters long.]
System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line) +278
System.Configuration.BaseConfigurationRecord.CreateSectionDefault(String configKey, Boolean getRuntimeObject, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object& result, Object& resultRuntimeObject) +59
System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) +1431
System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission) +56
System.Configuration.BaseConfigurationRecord.GetSection(String configKey) +8
System.Web.HttpContext.GetSection(String sectionName) +47
System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName) +39
System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey) +6
System.Configuration.ConfigurationManager.GetSection(String sectionName) +78
<my code that calls GetSection>

Why would the StringValidator be failing when the correctly-formatted value is set in my web.config? What am I overlooking?

解决方案

Because StringValidator is the cause of the exceptions I did some googling and apparently it's a bug in the .NET Framework: having a StringValidator with a Minimum Length argument means that it will always reject the empty string "" default value of the property, which would be set by the framework.

There are workarounds, but I couldn't justify spending time on them, so I stripped out the StringValidator attributes and my code works fine now.

Here's the QA I found: Why does StringValidator always fail for custom configuration section?

这篇关于ConfigurationSection - StringValidator失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆