ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-ConfigurationErrorsException“无效的键值”,我在做什么错?

2019-10-23 18:06:33  阅读:219  来源: 互联网

标签:app-config c net


我有一个简单的控制台应用程序,该应用程序旨在运行此命令时读取自定义配置异常(下面的代码),当我调用ConfigurationManager.GetSection时,我收到一条ConfigurationErrorsException和消息“ Invalid Key Value”.谁能看到我做错了吗?

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MySection" type="ConsoleApplication1.MySection, ConsoleApplication1" />
  </configSections>
  <MySection>
    <add name="MyName" value="MyValue" />
  </MySection>
</configuration>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Configuration;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MySection section = (MySection)ConfigurationManager.GetSection("MySection");

            Console.WriteLine("Done");
        }
    }

    public class MySection : ConfigurationSection
    {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public MyCollection Collection
        {
            get
            {
                return (MyCollection)this[""];
            }
        }
    }

    public class MyCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MyElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MyElement)element).Name;
        }
    }

    public class MyElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name { get; set; }

        [ConfigurationProperty("value")]
        public string Value { get; set; }


    }
}

例外

System.Configuration.ConfigurationErrorsException was unhandled
  Message=Invalid key value. (C:\Users\martin.brown\documents\visual studio 2010\Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.config line 7)
  Source=System.Configuration
  BareMessage=Invalid key value.
  Filename=C:\Users\martin.brown\documents\visual studio 2010\Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.config
  Line=7
  StackTrace:
       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, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, 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 ConsoleApplication1.Program.Main(String[] args) in C:\Users\martin.brown\documents\visual studio 2010\Projects\ConsoleApplication1\Program.cs:line 14
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

解决方法:

我没有看到自定义部分定义了您尝试执行的方式.但是,您可以通过进行一些小的更改来使其工作

从以下位置更改配置:

...
<MySection>
    <add name="MyName" value="MyValue" />
</MySection>
...

至:

...
<MySection>
    <MyElements>
        <add name="MyName" value="MyValue" />
    </MyElements>
</MySection>
...

然后稍微修改一下代码:

public class MySection : ConfigurationSection
{
    [ConfigurationProperty("MyElements", IsDefaultCollection = true)]
    public MyCollection Collection{get {return (MyCollection) this["MyElements"];}}
}

public class MyElement : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get {return (string) (base["name"]);}
        set {base["name"] = value;}
    }

    [ConfigurationProperty("value")]
    public string Value
    {
        get {return (string) (base["value"]);}
        set {base["value"] = value;}
    }
}

(MyCollection类保持不变)

这将起作用,并且还使您能够在配置中添加多个值:

...
<MySection>
    <MyElements>
        <add name="MyName1" value="MyValu1e" />
        <add name="MyName2" value="MyValue2" />
    </MyElements>
</MySection>
...

标签:app-config,c,net
来源: https://codeday.me/bug/20191023/1914714.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有