简体   繁体   English

是否可以在运行时切换app.config?

[英]Is switching app.config at runtime possible?

Is there a way at runtime to switch out an applications app.config (current.config to new.config, file for file). 有没有办法在运行时切换应用程序app.config(current.config到new.config,文件为file)。 I have a backup/restore process which needs to replace its own application.exe.config file. 我有一个备份/恢复过程,需要替换自己的application.exe.config文件。 I have seen this post but it does not answer how to do this at runtime. 我看过这篇文章,但它没有回答如何在运行时这样做。

Turns out I can swap the .config file for the new one and do a ConfigurationManager.RefreshSection(...) for each section. 事实证明,我可以将.config文件替换为新文件,并为每个部分执行ConfigurationManager.RefreshSection(...)。 It will update from the new .config file. 它将从新的.config文件更新。

Microsoft .NET's app.config is not designed for your scenario, as well as many others. Microsoft .NET的app.config不是为您的场景以及许多其他场景设计的。 I often encounter a similar need, so I have spent a lot of effort designing a solution. 我经常遇到类似的需求,因此我花了很多精力设计解决方案。

  1. Redesign to use app.config only as a configuration bootstrap: specify where to find the rest of the real configuration data. 重新设计为仅将app.config用作配置引导程序:指定在何处查找其他实际配置数据。 This information should almost never change, so there is no need to handle file watching or application restarts. 此信息几乎不会更改,因此无需处理文件监视或应用程序重新启动。

  2. Pick an alternate location for the real configuration data: a file, a database, perhaps even a web service. 为实际配置数据选择一个备用位置:文件,数据库,甚至是Web服务。 I prefer a database most of the time, so I create a configuration table with a simple structure that allows me to store my data. 我大多数时候都喜欢数据库,所以我创建了一个配置表,其结构简单,允许我存储我的数据。

  3. Implement a simple library to wrap your configuration access so that you have a simple API for the rest of your application (via dependency injection). 实现一个简单的库来包装您的配置访问,以便为您的应用程序的其余部分提供一个简单的API(通过依赖注入)。 Hide the usage of app.config as well as your real configuration storage location(s). 隐藏app.config的使用以及您的实际配置存储位置。 Since .NET is strongly-typed, make the configuration settings so--convert each string retrieved into the most-specific type available (URL, Int32, FileInfo, etc.). 由于.NET是强类型的,因此请进行配置设置 - 将检索到的每个字符串转换为可用的最具体类型(URL,Int32,FileInfo等)。

  4. Determine which configuration settings can be safely changed at runtime versus those that can't. 确定可以在运行时安全地更改哪些配置设置与不能安全更改的配置设置。 Typically, some settings need to change along with others, or it simply makes no sense to allow them to change at all. 通常,某些设置需要与其他设置一起更改,或者根本不允许它们进行更改。 If all your configuration data can safely change at runtime, then that makes things easy, but I HIGHLY doubt such a scenario. 如果所有配置数据都可以在运行时安全地更改,那么这会让事情变得简单,但我强烈怀疑这种情况。 Hide the changeability and interdependencies of the configuration settings to the extent possible. 尽可能隐藏配置设置的可变性和相互依赖性。

  5. Design the response to the unavailability of your real configuration data. 设计对实际配置数据不可用的响应。 I prefer to treat the absence of any configuration setting as a fatal error that aborts the application, unless I can identify a usable default. 我更喜欢将缺少任何配置设置视为中止应用程序的致命错误,除非我可以识别可用的默认值。 Likewise, I abort in the absence of the configuration storage container (file, database table, etc.). 同样,我在没有配置存储容器(文件,数据库表等)的情况下中止。

Enjoy, and best wishes. 享受,祝福。

Are you able to restart the application when you detect that you need to switch files? 当您检测到需要切换文件时,是否能够重新启动应用程序? If so, it's just a matter of switching the files and restarting. 如果是这样,只需切换文件并重新启动即可。 Now, the tricky bit is if .NET keeps the app.config file open while the program is running. 现在,棘手的一点是, 如果 .NET在程序运行时保持app.config文件打开。 I suspect it doesn't, but if the most obviously approach fails, I suggest you have a second application (cfgswitcher.exe) which waits for the process with a PID specified on the command line to terminate, then switches config files and relaunches the original process. 我怀疑它没有,但如果最明显的方法失败,我建议你有第二个应用程序(cfgswitcher.exe)等待进程,在命令行上指定的PID终止,然后切换配置文件并重新启动原始过程。 Then your app would just need to launch cfgswitcher.exe (passing in its own PID as a command line argument) and terminate. 然后你的应用程序只需要启动cfgswitcher.exe(传递自己的PID作为命令行参数)并终止。

As I say though, it's worth trying the more obvious approach first. 正如我所说,首先尝试更明显的方法是值得的。

EDIT: If you can't restart the application (or even part of it in a new AppDomain) then various aspects of app.config (assembly bindings etc) can't be changed. 编辑:如果您无法重新启动应用程序(或甚至是新的AppDomain中的部分应用程序),则无法更改app.config(程序集绑定等)的各个方面。 If you're only interested in your own configuration sections changing, then I suggest you store them in a separate config file and reload them whenever you want to. 如果您只对自己的配置部分更改感兴趣,那么我建议您将它们存储在单独的配置文件中,并在需要时重新加载它们。

Look at the events available to you on the ApplicationSettingsBase class. 查看ApplicationSettingsBase类上可用的事件。 There are PropertyChanged & SettingChanging that may give you what you need. 有PropertyChanged和SettingChanging可以为您提供所需。

You could also watch the file and if it has changed call the reload method to get the new settings. 您还可以查看该文件,如果已更改,请调用重新加载方法以获取新设置。

I don't think it is possible at all to switch the configuration at runtime without restarting, so if you can't apply Jon's approach, you should try to come up with an other approach. 我认为根本不可能在没有重新启动的情况下在运行时切换配置,所以如果你不能应用Jon的方法,你应该尝试提出另一种方法。

Anyway, maybe it's just me not having enough information about your scenario, but this kind of feels fishy. 无论如何,也许只是我没有足够的关于你的场景的信息,但这种感觉很可疑。

Are you sure that swapping the configuration file is the best way to achieve whatever requirement you need to meet? 您确定交换配置文件是实现您需要满足的任何要求的最佳方式吗? I mean, this is quite an uncommon thing. 我的意思是,这是一个非常罕见的事情。 If I were you, I would try to come up with some other approach. 如果我是你,我会尝试提出其他方法。

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

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