简体   繁体   English

自定义配置属性 - 已添加条目

[英]Custom configuration properties - Entry has already been added

I'm developing a windows service that reads information from the app.config at start-up which should allow us to change internal thread configuration without redeploying the service. 我正在开发一个Windows服务,它在启动时从app.config读取信息,这应该允许我们在不重新部署服务的情况下更改内部线程配置。

I created some custom configuration sections and elements as follows (implementation omitted): 我创建了一些自定义配置节和元素,如下所示(省略了实现):

public class MyConfigurationSection
{
    [ConfigurationProperty("threads")]
    [ConfigurationCollection(typeof(MyThreadCollection), AddItemName="addThread")>
    public MyThreadCollection threads { get; }
}

public class MyThreadCollection
{
    protected override void CreateNewElement();
    protected override object GetElementKey(ConfigurationElement element);
}

public class MyThreadElement
{
    [ConfigurationProperty("active", DefaultValue=true, IsRequired=false)>
    public bool active { get; set; }

    [ConfigurationProperty("batchSize", DefaultValue=10, IsRequired=false)>
    public int batchSize { get; set; }

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

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

    [ConfigurationProperty("connection", IsRequired=true)>
    public MyThreadConnectionElement connection { get; set; }
}

public class MyThreadConnectionElement
{
    [ConfigurationProperty("server", IsRequired=true)>
    public string server { get; set; }

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

    [ConfigurationProperty("timeout", DefaultValue=15, IsRequired=false)>
    public int timeout { get; set; }
}

Then I add some elements to the app.config as follows: 然后我将一些元素添加到app.config中,如下所示:

<configurationSection>
    <threads>
        <addThread
            active="True"
            batchSize="50"
            system="MySystem1"
            department="Department1">
            <connectionString
                server="MyServer"
                database="Database1" />
        </addThread>
        <addThread
            active="True"
            batchSize="30"
            system="MySystem2"
            department="Department2">
            <connectionString
                server="MyServer"
                database="Database2" />
        </addThread>
    </threads>
</configurationSection>

Everything works - configuration is read, threads are created, and the processes run. 一切正常 - 读取配置,创建线程,运行流程。

The problem is, I would like both these threads to have the same system name/value -- both should be MySystem -- but when I do that and run the program, I get a The entry 'MySystem' has already been added. 问题是,我希望这两个线程都具有相同的system名称/值 - 两者都应该是MySystem - 但是当我这样做并运行程序时,我得到了一个The entry 'MySystem' has already been added. exception. 例外。

I figured it might be because a property has to be explicitly configured to allow duplicates, but I don't know how and I couldn't find a property of the ConfigurationProperty class that might allow that, other than IsKey , but from its description it didn't seem like the answer, and trying it didn't solve the problem. 我想这可能是因为必须显式配置属性以允许重复,但我不知道如何和我找不到可能允许的属性的ConfigurationProperty类,除了IsKey ,但是从它的描述中似乎不是答案,尝试它并没有解决问题。 Am I on the right track here? 我在这里走在正确的轨道上吗?

Initially the system property was named name and I though that just maybe any property named name is treated as a unique identifier, so I changed it to system but it didn't change anything. 最初, system属性被命名为name ,我可能只是将名为name任何属性视为唯一标识符,因此我将其更改为system但它没有更改任何内容。

I tried the <clear /> tag as some other, similar posts suggested, without success. 我尝试了<clear />标签作为其他一些类似的帖子,没有成功。

Do I need to add another hierarchy to the configuration section -- Config -> Department -> Thread instead of Config -> Thread? 我是否需要在配置部分添加另一个层次结构 - 配置 - >部门 - >线程而不是配置 - >线程? I'd prefer to not take this approach. 我宁愿不采取这种方法。

Thanks for any and all input. 感谢任何和所有的输入。

I actually found the problem and solution quite some time ago, but forgot to post the answer; 我实际上很久以前就找到了问题和解决方案,但忘记发布答案; thanks @tote for reminding me. 谢谢@tote提醒我。

When implementing the ConfigurationElementCollection class, the GetElementKey(ConfigurationElement) method can be overridden. 实现ConfigurationElementCollection类时,可以重写GetElementKey(ConfigurationElement)方法。 Without immediately realising what the method is for I overrode it and simply returned the system property value, and, since more than one configuration element had the same system name, technically they had the same key, which is why the error occurred. 没有立即意识到方法是什么我覆盖它并简单地返回system属性值,并且,由于多个配置元素具有相同的系统名称,从技术上讲它们具有相同的密钥,这就是错误发生的原因。

The solution for me was to return the system and the department values as system.department which resulted in unique keys. 对我来说,解决方案是将systemdepartment值作为system.department返回,从而产生唯一键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM