读取自定义配置部分会返回“无效的键值”

发布于 2025-01-05 07:37:54 字数 4512 浏览 0 评论 0原文

我在使用 ConfigurationManager 读取 app.config 时遇到困难。我有一个自定义部分,它使用 ConfigurationElementCollection。

我的(精简)XML:

<configuration>
    <configSections>
        <sectionGroup name ="CustomerMatching">
            <section name="SearchWeight" type="BLL.Contracts.Helpers.CustomerMatching.SearchWeightSection, BLL.Contracts"/>
        </sectionGroup>
    </configSections>

    <CustomerMatching>
        <SearchWeight>
            <methods>
                <method SearchMethod="ByContactName" Weight="100"/> <!--Line 53, referenced in Error -->
                <method SearchMethod="ByBusinessName" Weight="250"/>
                <method SearchMethod="ByPostcode" Weight="250"/>
                <method SearchMethod="ByMobile" Weight="500"/>
                <method SearchMethod="ByLandline" Weight="500"/>
                <method SearchMethod="ByCompCharNo" Weight="850"/>
            </methods>
        </SearchWeight>
    </CustomerMatching>
</configuration>

我的配置类:

public class SearchWeightSection: ConfigurationSection
{
    [ConfigurationProperty("methods", IsRequired = true)]
    [ConfigurationCollection(typeof(SearchMethods), AddItemName = "method", CollectionType =  ConfigurationElementCollectionType.BasicMap)]
    public SearchMethods SearchMethods
    {
        get { return (SearchMethods) base["methods"]; }
    }
}

public class SearchMethods: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new Method();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        var method = (Method) element;

        return method.SearchMethod;
    }

    public new Method this[string index]
    {
        get { return (Method)BaseGet(index); }

    }
}

public class Method: ConfigurationElement
{
    [ConfigurationProperty("SearchMethod", IsKey = true, IsRequired = true)]
    public string SearchMethod { get; set; }

    [ConfigurationProperty("Weight", IsRequired = true)]
    public string Weight { get; set; }

    public Method(string searchMethod, string weight)
    {
        SearchMethod = searchMethod;
        Weight = weight;
    }

    public Method()
    {

    }
}

尝试使用它:

    [TestMethod]
    public void TestConfigReader()
    {
        var searchSection = (SearchWeightSection)ConfigurationManager.GetSection("CustomerMatching/SearchWeight");
        Assert.AreEqual(searchSection.SearchMethods["ByContactName"].Weight, "100");
    }

我的错误:

Test method BLL.UnitTests.Customer.CustomerMatchManagerTest.TestConfigReader threw exception: 
System.Configuration.ConfigurationErrorsException: Invalid key value. (C:\Users\michael\Documents\Visual Studio 2010\MyProject\BLL.UnitTests\bin\Debug\BLL.UnitTests.dll.config line 53)
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at BLL.UnitTests.Customer.CustomerMatchManagerTest.TestConfigReader() in CustomerMatchManagerTest.cs: line 41

谢谢。

I'm having difficulties reading an app.config using the ConfigurationManager. I have a custom section, which uses a ConfigurationElementCollection.

My (cut-down) XML:

<configuration>
    <configSections>
        <sectionGroup name ="CustomerMatching">
            <section name="SearchWeight" type="BLL.Contracts.Helpers.CustomerMatching.SearchWeightSection, BLL.Contracts"/>
        </sectionGroup>
    </configSections>

    <CustomerMatching>
        <SearchWeight>
            <methods>
                <method SearchMethod="ByContactName" Weight="100"/> <!--Line 53, referenced in Error -->
                <method SearchMethod="ByBusinessName" Weight="250"/>
                <method SearchMethod="ByPostcode" Weight="250"/>
                <method SearchMethod="ByMobile" Weight="500"/>
                <method SearchMethod="ByLandline" Weight="500"/>
                <method SearchMethod="ByCompCharNo" Weight="850"/>
            </methods>
        </SearchWeight>
    </CustomerMatching>
</configuration>

My Configuration classes:

public class SearchWeightSection: ConfigurationSection
{
    [ConfigurationProperty("methods", IsRequired = true)]
    [ConfigurationCollection(typeof(SearchMethods), AddItemName = "method", CollectionType =  ConfigurationElementCollectionType.BasicMap)]
    public SearchMethods SearchMethods
    {
        get { return (SearchMethods) base["methods"]; }
    }
}

public class SearchMethods: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new Method();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        var method = (Method) element;

        return method.SearchMethod;
    }

    public new Method this[string index]
    {
        get { return (Method)BaseGet(index); }

    }
}

public class Method: ConfigurationElement
{
    [ConfigurationProperty("SearchMethod", IsKey = true, IsRequired = true)]
    public string SearchMethod { get; set; }

    [ConfigurationProperty("Weight", IsRequired = true)]
    public string Weight { get; set; }

    public Method(string searchMethod, string weight)
    {
        SearchMethod = searchMethod;
        Weight = weight;
    }

    public Method()
    {

    }
}

Trying to use it:

    [TestMethod]
    public void TestConfigReader()
    {
        var searchSection = (SearchWeightSection)ConfigurationManager.GetSection("CustomerMatching/SearchWeight");
        Assert.AreEqual(searchSection.SearchMethods["ByContactName"].Weight, "100");
    }

My error:

Test method BLL.UnitTests.Customer.CustomerMatchManagerTest.TestConfigReader threw exception: 
System.Configuration.ConfigurationErrorsException: Invalid key value. (C:\Users\michael\Documents\Visual Studio 2010\MyProject\BLL.UnitTests\bin\Debug\BLL.UnitTests.dll.config line 53)
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, ref Object result, ref Object resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at BLL.UnitTests.Customer.CustomerMatchManagerTest.TestConfigReader() in CustomerMatchManagerTest.cs: line 41

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2025-01-12 07:37:54

除了 Method 配置元素本身之外,您的代码看起来绝对没问题。

由于它是配置元素,因此属性需要将数据存储在基类(ConfigurationElement)名称值集合中。否则 key 和 value 将不会被初始化。

public class Method: ConfigurationElement
{
    [ConfigurationProperty("SearchMethod", IsKey = true, IsRequired = true)]
    public string SearchMethod
    {
        get { return base["SearchMethod"] as string; }
        set { base["SearchMethod"] = value; }
    }

    [ConfigurationProperty("Weight", IsRequired = true)]
    public string Weight
    {
        get { return base["Weight"] as string; }
        set { base["Weight"] = value; }
    }

    //REST OF YOUR CLASS
}

希望有帮助。

Your code looks absolutely fine, apart from the Method configuration element itself.

Since it's a configuration element, properties need to store the data in the base class' (ConfigurationElement) name value collection. Otherwise key and value will not be initialized.

public class Method: ConfigurationElement
{
    [ConfigurationProperty("SearchMethod", IsKey = true, IsRequired = true)]
    public string SearchMethod
    {
        get { return base["SearchMethod"] as string; }
        set { base["SearchMethod"] = value; }
    }

    [ConfigurationProperty("Weight", IsRequired = true)]
    public string Weight
    {
        get { return base["Weight"] as string; }
        set { base["Weight"] = value; }
    }

    //REST OF YOUR CLASS
}

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文