无法让自定义配置部分工作

发布于 2024-12-12 22:26:50 字数 5471 浏览 12 评论 0原文

我正在尝试创建一个如下所示的自定义配置。

<configuration>
  <configSections>
    <section name="actions" type="ConfigurationTest.ActionsConfig, UnitTestExperiments" />
    <section name="action" type="ConfigurationTest.ActionConfig, UnitTestExperiments"/>
  </configSections>
  <actions poolSize="100">
    <action name="a1" impl="some.class1">
      <add name="key11" value="value11"/>
      <add name="key12" value="key12"/>
    </action>
    <action name="a2" impl="some.class2">
      <add name="key21" value="value21"/>
      <add name="key22" value="key22"/>
    </action>
  </actions>
</configuration>

这里 标签可以出现 N 次,并且每个操作都有一组唯一的键。我尝试了几种不同的解决方案和选项,但似乎我无法获得正确的类结构/映射。我希望得到一个可以这样调用的结构。

    using System;
    using System.Configuration;

    namespace ConfigurationTest
    {
        public class ActionsConfig : ConfigurationSection
        {
            [ConfigurationProperty("poolSize")]
            public string PoolSize
            {
                get { return (string)this["poolSize"]; }
            }

            [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
            public ActionList Instances
            {
                get { return (ActionList)this[""]; }
                set { this[""] = value; }
            }
        }

        public class ActionList : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new ActionConfig();
            }

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

        public class ActionConfig : ConfigurationSection
        {
            [ConfigurationProperty("name")]
            public string Name
            {
                get { return (string)this["name"]; }
            }

            [ConfigurationProperty("impl")]
            public string Impl
            {
                get { return (string)this["impl"]; }
            }

            [ConfigurationProperty("", IsDefaultCollection = true)]
            public NameValueConfigurationCollection Settings
            {
                get
                {
                    return (NameValueConfigurationCollection)base[""];
                }
            }
        }



   public class Program
        {
            static void Main(string[] args)
            {
                ActionsConfig ac = ConfigurationManager.GetSection("actions") as ActionsConfig;
                Console.WriteLine(ac.PoolSize);
                ActionConfig nameValueSection = ac.CurrentConfiguration.GetSection("action") as ActionConfig;
                NameValueConfigurationCollection settings = nameValueSection.Settings;
                foreach (var key in settings.AllKeys)
                {
                    Console.WriteLine(settings[key].Name + ": " + settings[key].Value);
                }
            }
        }
    }

异常跟踪如下。

    System.Configuration.ConfigurationErrorsException was unhandled
  Message=Unrecognized element 'action'. (UnitTestExperiments.vshost.exe.Config line 8)
  Source=System.Configuration
  BareMessage=Unrecognized element 'action'.
  Filename=UnitTestExperiments.vshost.exe.Config
  Line=8
  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.ConfigurationManager.GetSection(String sectionName)
       at ConfigurationTest.Program.Main(String[] args) in C:\Users\me\Documents\Visual Studio 2010\Projects\UnitTestExperiments\Program.cs:line 63
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       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: 

I am trying to create a custom configuration that looks like this.

<configuration>
  <configSections>
    <section name="actions" type="ConfigurationTest.ActionsConfig, UnitTestExperiments" />
    <section name="action" type="ConfigurationTest.ActionConfig, UnitTestExperiments"/>
  </configSections>
  <actions poolSize="100">
    <action name="a1" impl="some.class1">
      <add name="key11" value="value11"/>
      <add name="key12" value="key12"/>
    </action>
    <action name="a2" impl="some.class2">
      <add name="key21" value="value21"/>
      <add name="key22" value="key22"/>
    </action>
  </actions>
</configuration>

Here the <action> tag can occur N times and every action will have a unique set of keys. I tried a couple of different solutions and options but it seems like I cannot get the class structure/mapping correct. I am hoping to get a structure that I can call like this.

    using System;
    using System.Configuration;

    namespace ConfigurationTest
    {
        public class ActionsConfig : ConfigurationSection
        {
            [ConfigurationProperty("poolSize")]
            public string PoolSize
            {
                get { return (string)this["poolSize"]; }
            }

            [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
            public ActionList Instances
            {
                get { return (ActionList)this[""]; }
                set { this[""] = value; }
            }
        }

        public class ActionList : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new ActionConfig();
            }

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

        public class ActionConfig : ConfigurationSection
        {
            [ConfigurationProperty("name")]
            public string Name
            {
                get { return (string)this["name"]; }
            }

            [ConfigurationProperty("impl")]
            public string Impl
            {
                get { return (string)this["impl"]; }
            }

            [ConfigurationProperty("", IsDefaultCollection = true)]
            public NameValueConfigurationCollection Settings
            {
                get
                {
                    return (NameValueConfigurationCollection)base[""];
                }
            }
        }



   public class Program
        {
            static void Main(string[] args)
            {
                ActionsConfig ac = ConfigurationManager.GetSection("actions") as ActionsConfig;
                Console.WriteLine(ac.PoolSize);
                ActionConfig nameValueSection = ac.CurrentConfiguration.GetSection("action") as ActionConfig;
                NameValueConfigurationCollection settings = nameValueSection.Settings;
                foreach (var key in settings.AllKeys)
                {
                    Console.WriteLine(settings[key].Name + ": " + settings[key].Value);
                }
            }
        }
    }

Exception trace is below.

    System.Configuration.ConfigurationErrorsException was unhandled
  Message=Unrecognized element 'action'. (UnitTestExperiments.vshost.exe.Config line 8)
  Source=System.Configuration
  BareMessage=Unrecognized element 'action'.
  Filename=UnitTestExperiments.vshost.exe.Config
  Line=8
  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.ConfigurationManager.GetSection(String sectionName)
       at ConfigurationTest.Program.Main(String[] args) in C:\Users\me\Documents\Visual Studio 2010\Projects\UnitTestExperiments\Program.cs:line 63
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       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: 

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

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

发布评论

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

评论(2

£噩梦荏苒 2024-12-19 22:26:50

查看使用自定义配置设置。这有一个很好的例子。

更新

自定义ADD部分请查看示例.NET 2.0 .config 文件中的自定义配置部分

Take a look at use Custom Configuration Settings. This has a very good example.

Updates

Custom ADD sections take a look at the example Custom ConfigurationSections in .NET 2.0 .config Files

月野兔 2024-12-19 22:26:50

我从演示项目 这里 得到了确切的答案,

我必须扩展代码到一个额外的层,让它为我工作。

I got the EXACT answer from the demo project Here

I had to extend the code to an extra layer to make it work for me.

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