ConfigurationManager.GetSection 跳过重复项
长话短说,.Net ConfigurationManager.GetSection
方法会跳过重复项,我正在寻找处理此问题的最佳实践。我有一个配置文件,其中包含如下内容:
<views>
<view name="BusinessUnitsView" Desc="desc1"/>
<view name="BusinessUnitsView" Desc="desc2"/>
</views>
我有一个配置加载到的对象图,其中集合派生自我们的 ConfigurationElementCollection
的派生版本。加载上述内容后,图中的视图集合中只有一项 - 我的理解是,这只是 ConfigurationManager
处理重复项的方式。最后一个具有相同密钥的项目获胜。
我可以在使用 BaseAdd(element, true) 找到重复项时引发异常。但是,如果可能的话,我希望对象完全加载重复项,因为配置是在服务层中读取的,并且我希望检测并处理服务消费端的问题。
我是否可以选择修改添加到集合工作的方式?
Long story short, the .Net ConfigurationManager.GetSection
method skips duplicates, and I'm looking for a best practice for handling this. I have a config file that includes something like this:
<views>
<view name="BusinessUnitsView" Desc="desc1"/>
<view name="BusinessUnitsView" Desc="desc2"/>
</views>
I have a graph of objects the config loads onto where collections derive from our derived version of ConfigurationElementCollection
. The views collection in the graph only has one item in it after loading the above--my understanding is that this is simply the way the ConfigurationManager
handled duplicates. The last item with the same key wins.
I could throw an exception on a duplicate being found by using BaseAdd(element, true). However, if possible I'd like to get the object completely loaded WITH duplicates, as the config gets read in a service layer, and I'd like to detect and deal with the problem on the consuming side of the service.
Do I have any options for modifying the way adds to the collection work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建自己的
ConfigurationSection
。请参阅此处或这里(第二个链接的方法已被弃用,但它可能仍然会脱落一些轻)这允许您将内部配置变量表示为集合,并设置诸如
DefaultValue
和IsRequired
之类的属性。You will need to create your own
ConfigurationSection
. See here or here (the second link's method has been deprecated, but it may still shed some light)This allows you to represent internal configuration variables as collections, and set properties like
DefaultValue
andIsRequired
.也许您只想迭代一个列表,但配置的主要思想是您可以
只允许一个答案。
Maybe you only want to iterate over a list but the main idea of the config is that you can do
That only allows one answer.