如何让 ConfigurationManager 从多个文件加载应用程序设置?
我正在编写与第三方应用程序互操作的应用程序。 该应用程序通过 DLL 中的方法向开发人员公开 API。 不久前,该应用程序的供应商开始将他们自己的 .NET 组件集成到他们的程序中,当他们这样做时,他们决定他们的组件应该使用 ConfigurationManager
在运行时获取设置。
这意味着:他们的程序 foo.exe
调用 fooengine.dll
,并从 foo.exe.config
读取其设置。 我的程序 bar.exe
也调用 fooengine.dll
,并从 bar.exe.config
读取其设置。
嗯,这完全是错误的。 但我该如何解决它呢?
简单的解决方法是在 bar.exe.config
中复制 foo.exe.config
的设置。 这会起作用,但它很愚蠢。 这意味着从管理的角度来看,必须在 N 个不同的文件中维护给定的设置。 那迟早会失败。
我尝试将 configSource
属性放在配置文件的 appSettings
部分。 (碰巧,我使用 applicationSettings
部分进行我的设置,而他们使用 appSettings
部分进行他们的设置,所以我可以接受简单地获取该部分来自不同的文件。)但是 ConfigurationManager
不喜欢这样:它希望 configSource
中的路径不仅相对于,而且低于我的程序的目录。
我可以将它们的设置文件读取到 XmlDocument
中,然后自己进行设置。 但现在我将我的代码与其实现紧密耦合; 如果他们发布一个新版本,将设置移动到 applicationSettings
部分(现在是 2009 年,他们应该在这个位置),我的代码就会崩溃。
还有其他办法吗?
I'm writing applications that interoperate with a third-party application. This application exposes an API to developers via methods in a DLL. Some time ago, the vendor of this app started integrating their own .NET components into their program, and when they did, they decided that their components should use the ConfigurationManager
to get settings at runtime.
What this means: their program, foo.exe
, calls fooengine.dll
, and it reads its settings from foo.exe.config
. My program, bar.exe
, also calls fooengine.dll
, and it reads its settings from bar.exe.config
.
Well, that's just plain wrong. But how do I fix it?
The simple workaround is to replicate foo.exe.config
's settings in bar.exe.config
. That'll work, but it's stupid. It means that from an administrative standpoint, a given setting has to be maintained in N different files. That's going to fail sooner or later.
I tried putting a configSource
attribute on the appSettings
section in my config file. (As it happens, I'm using the applicationSettings
section for my settings, and they're using the appSettings
section for theirs, so I can live with simply getting that section from a different file.) But the ConfigurationManager
doesn't like that: it wants the path in configSource
to be not only relative to but below my program's directory.
I can physically read their settings file into an XmlDocument
and then set them myself. But now I'm tightly coupling my code to their implementation; if they put out a new release that moves the settings to the applicationSettings
section (which is where they should be now that it's 2009), my code will break.
Is there another way out of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想我已经找到了答案,至少对于我这个问题的特定版本来说是这样。
.NET 2.0
ConfigurationManager
支持appSettings
元素上的file
属性。 这使您可以从外部文件获取该元素的内容。 所以我要做的是:从
foo.exe.config
中剪切appSettings
元素,并将其粘贴到该目录中的另一个文件中,例如,appSettings.xml
。向
foo.exe.config
添加新元素:
。向
bar.exe.config
添加一个元素:这有效。 但它之所以有效,是因为我的程序根本不使用
appSettings
。Okay, I think I've found the answer, at least for my specific version of this problem.
The .NET 2.0
ConfigurationManager
supports afile
attribute on theappSettings
element. This lets you get the contents of that element from an external file. So what I do is:Cut the
appSettings
element out offoo.exe.config
and paste it into another file in that directory called, let's say,appSettings.xml
.Add a new element to
foo.exe.config
:<appSettings file="appSettings.xml"/>
.Add an element to
bar.exe.config
:<appSettings file="c:\program files\foo\appSettings.xml"/>
This works. But it only works because my program doesn't use
appSettings
at all.您可以加载其配置文件。
You could just load their configuration file.